blob: 33c818da9919785f37de4bd293319eda4aea2ba5 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/app/webrtc/webrtcsession.h"
29
pbos@webrtc.org371243d2014-03-07 15:22:04 +000030#include <limits.h>
31
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032#include <algorithm>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033#include <vector>
deadbeefcbecd352015-09-23 11:50:27 -070034#include <set>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035
36#include "talk/app/webrtc/jsepicecandidate.h"
37#include "talk/app/webrtc/jsepsessiondescription.h"
38#include "talk/app/webrtc/mediaconstraintsinterface.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039#include "talk/app/webrtc/peerconnectioninterface.h"
deadbeefab9b2d12015-10-14 11:33:11 -070040#include "talk/app/webrtc/sctputils.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000041#include "talk/app/webrtc/webrtcsessiondescriptionfactory.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042#include "talk/media/base/constants.h"
43#include "talk/media/base/videocapturer.h"
44#include "talk/session/media/channel.h"
45#include "talk/session/media/channelmanager.h"
46#include "talk/session/media/mediasession.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000047#include "webrtc/base/basictypes.h"
jbauchac8869e2015-07-03 01:36:14 -070048#include "webrtc/base/checks.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000049#include "webrtc/base/helpers.h"
50#include "webrtc/base/logging.h"
51#include "webrtc/base/stringencode.h"
52#include "webrtc/base/stringutils.h"
stefanc1aeaf02015-10-15 07:26:07 -070053#include "webrtc/call.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000054#include "webrtc/p2p/base/portallocator.h"
stefanc1aeaf02015-10-15 07:26:07 -070055#include "webrtc/p2p/base/transportchannel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056
57using cricket::ContentInfo;
58using cricket::ContentInfos;
59using cricket::MediaContentDescription;
60using cricket::SessionDescription;
61using cricket::TransportInfo;
62
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070063using cricket::LOCAL_PORT_TYPE;
64using cricket::STUN_PORT_TYPE;
65using cricket::RELAY_PORT_TYPE;
66using cricket::PRFLX_PORT_TYPE;
67
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068namespace webrtc {
69
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000071const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
72 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000073const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074const char kInvalidCandidates[] = "Description contains invalid candidates.";
75const char kInvalidSdp[] = "Invalid session description.";
76const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000077 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
78const char kPushDownTDFailed[] =
79 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000080const char kSdpWithoutDtlsFingerprint[] =
81 "Called with SDP without DTLS fingerprint.";
82const char kSdpWithoutSdesCrypto[] =
83 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000084const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000085 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000087const char kSessionErrorDesc[] = "Session error description: ";
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000088const char kDtlsSetupFailureRtp[] =
89 "Couldn't set up DTLS-SRTP on RTP channel.";
90const char kDtlsSetupFailureRtcp[] =
91 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeefcbecd352015-09-23 11:50:27 -070092const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +000093const int kMaxUnsignalledRecvStreams = 20;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070095IceCandidatePairType GetIceCandidatePairCounter(
96 const cricket::Candidate& local,
97 const cricket::Candidate& remote) {
98 const auto& l = local.type();
99 const auto& r = remote.type();
100 const auto& host = LOCAL_PORT_TYPE;
101 const auto& srflx = STUN_PORT_TYPE;
102 const auto& relay = RELAY_PORT_TYPE;
103 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -0700104 if (l == host && r == host) {
105 bool local_private = IPIsPrivate(local.address().ipaddr());
106 bool remote_private = IPIsPrivate(remote.address().ipaddr());
107 if (local_private) {
108 if (remote_private) {
109 return kIceCandidatePairHostPrivateHostPrivate;
110 } else {
111 return kIceCandidatePairHostPrivateHostPublic;
112 }
113 } else {
114 if (remote_private) {
115 return kIceCandidatePairHostPublicHostPrivate;
116 } else {
117 return kIceCandidatePairHostPublicHostPublic;
118 }
119 }
120 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700121 if (l == host && r == srflx)
122 return kIceCandidatePairHostSrflx;
123 if (l == host && r == relay)
124 return kIceCandidatePairHostRelay;
125 if (l == host && r == prflx)
126 return kIceCandidatePairHostPrflx;
127 if (l == srflx && r == host)
128 return kIceCandidatePairSrflxHost;
129 if (l == srflx && r == srflx)
130 return kIceCandidatePairSrflxSrflx;
131 if (l == srflx && r == relay)
132 return kIceCandidatePairSrflxRelay;
133 if (l == srflx && r == prflx)
134 return kIceCandidatePairSrflxPrflx;
135 if (l == relay && r == host)
136 return kIceCandidatePairRelayHost;
137 if (l == relay && r == srflx)
138 return kIceCandidatePairRelaySrflx;
139 if (l == relay && r == relay)
140 return kIceCandidatePairRelayRelay;
141 if (l == relay && r == prflx)
142 return kIceCandidatePairRelayPrflx;
143 if (l == prflx && r == host)
144 return kIceCandidatePairPrflxHost;
145 if (l == prflx && r == srflx)
146 return kIceCandidatePairPrflxSrflx;
147 if (l == prflx && r == relay)
148 return kIceCandidatePairPrflxRelay;
149 return kIceCandidatePairMax;
150}
151
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152// Compares |answer| against |offer|. Comparision is done
153// for number of m-lines in answer against offer. If matches true will be
154// returned otherwise false.
155static bool VerifyMediaDescriptions(
156 const SessionDescription* answer, const SessionDescription* offer) {
157 if (offer->contents().size() != answer->contents().size())
158 return false;
159
160 for (size_t i = 0; i < offer->contents().size(); ++i) {
161 if ((offer->contents()[i].name) != answer->contents()[i].name) {
162 return false;
163 }
wu@webrtc.org4e393072014-04-07 17:04:35 +0000164 const MediaContentDescription* offer_mdesc =
165 static_cast<const MediaContentDescription*>(
166 offer->contents()[i].description);
167 const MediaContentDescription* answer_mdesc =
168 static_cast<const MediaContentDescription*>(
169 answer->contents()[i].description);
170 if (offer_mdesc->type() != answer_mdesc->type()) {
171 return false;
172 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173 }
174 return true;
175}
176
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177// Checks that each non-rejected content has SDES crypto keys or a DTLS
178// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
179// keys, will be caught in Transport negotiation, and backstopped by Channel's
180// |secure_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000181static bool VerifyCrypto(const SessionDescription* desc,
182 bool dtls_enabled,
183 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184 const ContentInfos& contents = desc->contents();
185 for (size_t index = 0; index < contents.size(); ++index) {
186 const ContentInfo* cinfo = &contents[index];
187 if (cinfo->rejected) {
188 continue;
189 }
190
191 // If the content isn't rejected, crypto must be present.
192 const MediaContentDescription* media =
193 static_cast<const MediaContentDescription*>(cinfo->description);
194 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
195 if (!media || !tinfo) {
196 // Something is not right.
197 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000198 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 return false;
200 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000201 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000202 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000203 LOG(LS_WARNING) <<
204 "Session description must have DTLS fingerprint if DTLS enabled.";
205 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000206 return false;
207 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000208 } else {
209 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000210 LOG(LS_WARNING) <<
211 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000212 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000213 return false;
214 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215 }
216 }
217
218 return true;
219}
220
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000221// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
222static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
223 const ContentInfos& contents = desc->contents();
224 for (size_t index = 0; index < contents.size(); ++index) {
225 const ContentInfo* cinfo = &contents[index];
226 if (cinfo->rejected) {
227 continue;
228 }
229
230 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
231 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
232 if (!tinfo) {
233 // Something is not right.
234 LOG(LS_ERROR) << kInvalidSdp;
235 return false;
236 }
237 if (tinfo->description.ice_ufrag.empty() ||
238 tinfo->description.ice_pwd.empty()) {
239 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
240 return false;
241 }
242 }
243 return true;
244}
245
wu@webrtc.org91053e72013-08-10 07:18:04 +0000246// Forces |sdesc->crypto_required| to the appropriate state based on the
247// current security policy, to ensure a failure occurs if there is an error
248// in crypto negotiation.
249// Called when processing the local session description.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000250static void UpdateSessionDescriptionSecurePolicy(cricket::CryptoType type,
251 SessionDescription* sdesc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000252 if (!sdesc) {
253 return;
254 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255
wu@webrtc.org91053e72013-08-10 07:18:04 +0000256 // Updating the |crypto_required_| in MediaContentDescription to the
257 // appropriate state based on the current security policy.
258 for (cricket::ContentInfos::iterator iter = sdesc->contents().begin();
259 iter != sdesc->contents().end(); ++iter) {
260 if (cricket::IsMediaContent(&*iter)) {
261 MediaContentDescription* mdesc =
262 static_cast<MediaContentDescription*> (iter->description);
263 if (mdesc) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000264 mdesc->set_crypto_required(type);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000265 }
266 }
267 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268}
269
Peter Boström0c4e06b2015-10-07 12:23:21 +0200270static bool GetAudioSsrcByTrackId(const SessionDescription* session_description,
271 const std::string& track_id,
272 uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273 const cricket::ContentInfo* audio_info =
274 cricket::GetFirstAudioContent(session_description);
275 if (!audio_info) {
276 LOG(LS_ERROR) << "Audio not used in this call";
277 return false;
278 }
279
280 const cricket::MediaContentDescription* audio_content =
281 static_cast<const cricket::MediaContentDescription*>(
282 audio_info->description);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000283 const cricket::StreamParams* stream =
284 cricket::GetStreamByIds(audio_content->streams(), "", track_id);
285 if (!stream) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286 return false;
287 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000288
289 *ssrc = stream->first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290 return true;
291}
292
293static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200294 uint32_t ssrc,
295 std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000296 ASSERT(track_id != NULL);
297
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000298 const cricket::ContentInfo* audio_info =
299 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000300 if (audio_info) {
301 const cricket::MediaContentDescription* audio_content =
302 static_cast<const cricket::MediaContentDescription*>(
303 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000304
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000305 const auto* found =
306 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
307 if (found) {
308 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000309 return true;
310 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311 }
312
313 const cricket::ContentInfo* video_info =
314 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000315 if (video_info) {
316 const cricket::MediaContentDescription* video_content =
317 static_cast<const cricket::MediaContentDescription*>(
318 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000320 const auto* found =
321 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
322 if (found) {
323 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000324 return true;
325 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326 }
327 return false;
328}
329
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000330static bool BadSdp(const std::string& source,
331 const std::string& type,
332 const std::string& reason,
333 std::string* err_desc) {
334 std::ostringstream desc;
deadbeefd59daf82015-10-14 15:02:44 -0700335 desc << "Failed to set " << source;
336 if (!type.empty()) {
337 desc << " " << type;
338 }
339 desc << " sdp: " << reason;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000340
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000342 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000344 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345 return false;
346}
347
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000348static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000349 const std::string& type,
350 const std::string& reason,
351 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000353 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000354 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000355 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356 }
357}
358
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000359static bool BadLocalSdp(const std::string& type,
360 const std::string& reason,
361 std::string* err_desc) {
362 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
363}
364
365static bool BadRemoteSdp(const std::string& type,
366 const std::string& reason,
367 std::string* err_desc) {
368 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
369}
370
371static bool BadOfferSdp(cricket::ContentSource source,
372 const std::string& reason,
373 std::string* err_desc) {
374 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
375}
376
377static bool BadPranswerSdp(cricket::ContentSource source,
378 const std::string& reason,
379 std::string* err_desc) {
380 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
381 reason, err_desc);
382}
383
384static bool BadAnswerSdp(cricket::ContentSource source,
385 const std::string& reason,
386 std::string* err_desc) {
387 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388}
389
deadbeefd59daf82015-10-14 15:02:44 -0700390#define GET_STRING_OF_STATE(state) \
391 case webrtc::WebRtcSession::state: \
392 result = #state; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393 break;
394
deadbeefd59daf82015-10-14 15:02:44 -0700395static std::string GetStateString(webrtc::WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396 std::string result;
397 switch (state) {
398 GET_STRING_OF_STATE(STATE_INIT)
deadbeefd59daf82015-10-14 15:02:44 -0700399 GET_STRING_OF_STATE(STATE_SENTOFFER)
400 GET_STRING_OF_STATE(STATE_RECEIVEDOFFER)
401 GET_STRING_OF_STATE(STATE_SENTPRANSWER)
402 GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000403 GET_STRING_OF_STATE(STATE_INPROGRESS)
deadbeefd59daf82015-10-14 15:02:44 -0700404 GET_STRING_OF_STATE(STATE_CLOSED)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405 default:
406 ASSERT(false);
407 break;
408 }
409 return result;
410}
411
deadbeefd59daf82015-10-14 15:02:44 -0700412#define GET_STRING_OF_ERROR_CODE(err) \
413 case webrtc::WebRtcSession::err: \
414 result = #err; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000415 break;
416
deadbeefd59daf82015-10-14 15:02:44 -0700417static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000418 std::string result;
419 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000420 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000421 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
422 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000423 default:
deadbeefd59daf82015-10-14 15:02:44 -0700424 RTC_DCHECK(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000425 break;
426 }
427 return result;
428}
429
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000430static std::string MakeErrorString(const std::string& error,
431 const std::string& desc) {
432 std::ostringstream ret;
433 ret << error << " " << desc;
434 return ret.str();
435}
436
437static std::string MakeTdErrorString(const std::string& desc) {
438 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439}
440
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000441// Set |option| to the highest-priority value of |key| in the optional
442// constraints if the key is found and has a valid value.
kwiberg102c6a62015-10-30 02:47:38 -0700443template <typename T>
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000444static void SetOptionFromOptionalConstraint(
445 const MediaConstraintsInterface* constraints,
kwiberg102c6a62015-10-30 02:47:38 -0700446 const std::string& key,
Karl Wibergbe579832015-11-10 22:34:18 +0100447 rtc::Optional<T>* option) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000448 if (!constraints) {
449 return;
450 }
451 std::string string_value;
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000452 T value;
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000453 if (constraints->GetOptional().FindFirst(key, &string_value)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000454 if (rtc::FromString(string_value, &value)) {
Karl Wibergbe579832015-11-10 22:34:18 +0100455 *option = rtc::Optional<T>(value);
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000456 }
457 }
458}
459
Peter Boström0c4e06b2015-10-07 12:23:21 +0200460uint32_t ConvertIceTransportTypeToCandidateFilter(
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000461 PeerConnectionInterface::IceTransportsType type) {
462 switch (type) {
463 case PeerConnectionInterface::kNone:
464 return cricket::CF_NONE;
465 case PeerConnectionInterface::kRelay:
466 return cricket::CF_RELAY;
467 case PeerConnectionInterface::kNoHost:
468 return (cricket::CF_ALL & ~cricket::CF_HOST);
469 case PeerConnectionInterface::kAll:
470 return cricket::CF_ALL;
471 default: ASSERT(false);
472 }
473 return cricket::CF_NONE;
474}
475
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476// Help class used to remember if a a remote peer has requested ice restart by
477// by sending a description with new ice ufrag and password.
478class IceRestartAnswerLatch {
479 public:
480 IceRestartAnswerLatch() : ice_restart_(false) { }
481
wu@webrtc.org91053e72013-08-10 07:18:04 +0000482 // Returns true if CheckForRemoteIceRestart has been called with a new session
483 // description where ice password and ufrag has changed since last time
484 // Reset() was called.
485 bool Get() const {
486 return ice_restart_;
487 }
488
489 void Reset() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490 if (ice_restart_) {
491 ice_restart_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493 }
494
honghaiz503726c2015-07-31 12:37:38 -0700495 bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
496 const SessionDescriptionInterface* new_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 if (!old_desc || new_desc->type() != SessionDescriptionInterface::kOffer) {
honghaiz503726c2015-07-31 12:37:38 -0700498 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 }
500 const SessionDescription* new_sd = new_desc->description();
501 const SessionDescription* old_sd = old_desc->description();
502 const ContentInfos& contents = new_sd->contents();
503 for (size_t index = 0; index < contents.size(); ++index) {
504 const ContentInfo* cinfo = &contents[index];
505 if (cinfo->rejected) {
506 continue;
507 }
508 // If the content isn't rejected, check if ufrag and password has
509 // changed.
510 const cricket::TransportDescription* new_transport_desc =
511 new_sd->GetTransportDescriptionByName(cinfo->name);
512 const cricket::TransportDescription* old_transport_desc =
513 old_sd->GetTransportDescriptionByName(cinfo->name);
514 if (!new_transport_desc || !old_transport_desc) {
515 // No transport description exist. This is not an ice restart.
516 continue;
517 }
jiayl@webrtc.orgdb397e52014-06-20 16:32:09 +0000518 if (cricket::IceCredentialsChanged(old_transport_desc->ice_ufrag,
519 old_transport_desc->ice_pwd,
520 new_transport_desc->ice_ufrag,
521 new_transport_desc->ice_pwd)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522 LOG(LS_INFO) << "Remote peer request ice restart.";
523 ice_restart_ = true;
honghaiz503726c2015-07-31 12:37:38 -0700524 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525 }
526 }
honghaiz503726c2015-07-31 12:37:38 -0700527 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 }
529
530 private:
531 bool ice_restart_;
532};
533
stefanc1aeaf02015-10-15 07:26:07 -0700534WebRtcSession::WebRtcSession(webrtc::MediaControllerInterface* media_controller,
deadbeefab9b2d12015-10-14 11:33:11 -0700535 rtc::Thread* signaling_thread,
536 rtc::Thread* worker_thread,
537 cricket::PortAllocator* port_allocator)
deadbeefd59daf82015-10-14 15:02:44 -0700538 : signaling_thread_(signaling_thread),
539 worker_thread_(worker_thread),
540 port_allocator_(port_allocator),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541 // RFC 3264: The numeric value of the session id and version in the
542 // o line MUST be representable with a "64 bit signed integer".
543 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700544 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
545 transport_controller_(new cricket::TransportController(signaling_thread,
546 worker_thread,
547 port_allocator)),
stefanc1aeaf02015-10-15 07:26:07 -0700548 media_controller_(media_controller),
549 channel_manager_(media_controller_->channel_manager()),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550 ice_observer_(NULL),
551 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700552 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000554 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000556 ice_restart_latch_(new IceRestartAnswerLatch),
557 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700558 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
559 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700560 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700561 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700562 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700563 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700564 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700565 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700566 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567}
568
569WebRtcSession::~WebRtcSession() {
tommi0f620f42015-07-09 03:25:02 -0700570 ASSERT(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000571 // Destroy video_channel_ first since it may have a pointer to the
572 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000573 if (video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574 SignalVideoChannelDestroyed();
575 channel_manager_->DestroyVideoChannel(video_channel_.release());
576 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000577 if (voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000578 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200579 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000580 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000581 if (data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582 SignalDataChannelDestroyed();
583 channel_manager_->DestroyDataChannel(data_channel_.release());
584 }
deadbeefd59daf82015-10-14 15:02:44 -0700585
586 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587}
588
wu@webrtc.org91053e72013-08-10 07:18:04 +0000589bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000590 const PeerConnectionFactoryInterface::Options& options,
deadbeefcbecd352015-09-23 11:50:27 -0700591 const MediaConstraintsInterface* constraints,
Henrik Boström5e56c592015-08-11 10:33:13 +0200592 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
Henrik Lundin64dad832015-05-11 12:44:23 +0200593 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
594 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700595 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
qiangchen444682a2015-11-24 18:07:56 -0800596 video_options_.disable_prerenderer_smoothing =
597 rtc::Optional<bool>(rtc_configuration.disable_prerenderer_smoothing);
deadbeefd59daf82015-10-14 15:02:44 -0700598 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000599
Henrik Boström87713d02015-08-25 09:53:21 +0200600 // Obtain a certificate from RTCConfiguration if any were provided (optional).
601 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
602 if (!rtc_configuration.certificates.empty()) {
603 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
604 // just picking the first one. The decision should be made based on the DTLS
605 // handshake. The DTLS negotiations need to know about all certificates.
606 certificate = rtc_configuration.certificates[0];
607 }
608
honghaiz1f429e32015-09-28 07:57:34 -0700609 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700610
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611 // TODO(perkj): Take |constraints| into consideration. Return false if not all
612 // mandatory constraints can be fulfilled. Note that |constraints|
613 // can be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 bool value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000615
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000616 if (options.disable_encryption) {
617 dtls_enabled_ = false;
618 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200619 // Enable DTLS by default if we have an identity store or a certificate.
620 dtls_enabled_ = (dtls_identity_store || certificate);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000621 // |constraints| can override the default |dtls_enabled_| value.
deadbeefcbecd352015-09-23 11:50:27 -0700622 if (FindConstraint(constraints, MediaConstraintsInterface::kEnableDtlsSrtp,
623 &value, nullptr)) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000624 dtls_enabled_ = value;
625 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000626 }
627
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000628 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000629 // It takes precendence over the disable_sctp_data_channels
630 // PeerConnectionFactoryInterface::Options.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631 if (FindConstraint(
632 constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
633 &value, NULL) && value) {
634 LOG(LS_INFO) << "Allowing RTP data engine.";
635 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000636 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000637 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000638 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000639 LOG(LS_INFO) << "Allowing SCTP data engine.";
640 data_channel_type_ = cricket::DCT_SCTP;
641 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643
wu@webrtc.orgde305012013-10-31 15:40:38 +0000644 // Find DSCP constraint.
645 if (FindConstraint(
646 constraints,
647 MediaConstraintsInterface::kEnableDscp,
648 &value, NULL)) {
Karl Wibergbe579832015-11-10 22:34:18 +0100649 audio_options_.dscp = rtc::Optional<bool>(value);
650 video_options_.dscp = rtc::Optional<bool>(value);
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000651 }
652
653 // Find Suspend Below Min Bitrate constraint.
654 if (FindConstraint(
655 constraints,
656 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
657 &value,
658 NULL)) {
Karl Wibergbe579832015-11-10 22:34:18 +0100659 video_options_.suspend_below_min_bitrate = rtc::Optional<bool>(value);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000660 }
661
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000662 SetOptionFromOptionalConstraint(constraints,
663 MediaConstraintsInterface::kScreencastMinBitrate,
664 &video_options_.screencast_min_bitrate);
665
666 // Find constraints for cpu overuse detection.
667 SetOptionFromOptionalConstraint(constraints,
668 MediaConstraintsInterface::kCpuUnderuseThreshold,
669 &video_options_.cpu_underuse_threshold);
670 SetOptionFromOptionalConstraint(constraints,
671 MediaConstraintsInterface::kCpuOveruseThreshold,
672 &video_options_.cpu_overuse_threshold);
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000673 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000674 MediaConstraintsInterface::kCpuOveruseDetection,
675 &video_options_.cpu_overuse_detection);
676 SetOptionFromOptionalConstraint(constraints,
677 MediaConstraintsInterface::kCpuOveruseEncodeUsage,
678 &video_options_.cpu_overuse_encode_usage);
679 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000680 MediaConstraintsInterface::kCpuUnderuseEncodeRsdThreshold,
681 &video_options_.cpu_underuse_encode_rsd_threshold);
682 SetOptionFromOptionalConstraint(constraints,
683 MediaConstraintsInterface::kCpuOveruseEncodeRsdThreshold,
684 &video_options_.cpu_overuse_encode_rsd_threshold);
buildbot@webrtc.orgdb563902014-06-13 13:05:48 +0000685
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000686 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000687 MediaConstraintsInterface::kNumUnsignalledRecvStreams,
688 &video_options_.unsignalled_recv_stream_limit);
kwiberg102c6a62015-10-30 02:47:38 -0700689 if (video_options_.unsignalled_recv_stream_limit) {
Karl Wibergbe579832015-11-10 22:34:18 +0100690 video_options_.unsignalled_recv_stream_limit = rtc::Optional<int>(
kwiberg102c6a62015-10-30 02:47:38 -0700691 std::max(0, std::min(kMaxUnsignalledRecvStreams,
692 *video_options_.unsignalled_recv_stream_limit)));
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000693 }
694
695 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000696 MediaConstraintsInterface::kHighStartBitrate,
697 &video_options_.video_start_bitrate);
wu@webrtc.orgcfe5e9c2014-03-27 17:03:58 +0000698
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000699 SetOptionFromOptionalConstraint(constraints,
700 MediaConstraintsInterface::kCombinedAudioVideoBwe,
701 &audio_options_.combined_audio_video_bwe);
702
kwiberg102c6a62015-10-30 02:47:38 -0700703 audio_options_.audio_jitter_buffer_max_packets =
Karl Wibergbe579832015-11-10 22:34:18 +0100704 rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200705
Karl Wibergbe579832015-11-10 22:34:18 +0100706 audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(
707 rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200708
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 const cricket::VideoCodec default_codec(
710 JsepSessionDescription::kDefaultVideoCodecId,
711 JsepSessionDescription::kDefaultVideoCodecName,
712 JsepSessionDescription::kMaxVideoCodecWidth,
713 JsepSessionDescription::kMaxVideoCodecHeight,
714 JsepSessionDescription::kDefaultVideoCodecFramerate,
715 JsepSessionDescription::kDefaultVideoCodecPreference);
716 channel_manager_->SetDefaultVideoEncoderConfig(
717 cricket::VideoEncoderConfig(default_codec));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000718
Henrik Boström87713d02015-08-25 09:53:21 +0200719 if (!dtls_enabled_) {
720 // Construct with DTLS disabled.
721 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700722 signaling_thread(), channel_manager_, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200723 } else {
724 // Construct with DTLS enabled.
725 if (!certificate) {
726 // Use the |dtls_identity_store| to generate a certificate.
henrikg91d6ede2015-09-17 00:24:34 -0700727 RTC_DCHECK(dtls_identity_store);
Henrik Boström87713d02015-08-25 09:53:21 +0200728 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700729 signaling_thread(), channel_manager_, dtls_identity_store.Pass(),
730 this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200731 } else {
732 // Use the already generated certificate.
733 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700734 signaling_thread(), channel_manager_, certificate, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200735 }
736 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000737
Henrik Boströmd8281982015-08-27 10:12:24 +0200738 webrtc_session_desc_factory_->SignalCertificateReady.connect(
739 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000740
wu@webrtc.org97077a32013-10-25 21:18:33 +0000741 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000742 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000743 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000744 port_allocator()->set_candidate_filter(
Henrik Lundin64dad832015-05-11 12:44:23 +0200745 ConvertIceTransportTypeToCandidateFilter(rtc_configuration.type));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700746
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 return true;
748}
749
deadbeefd59daf82015-10-14 15:02:44 -0700750void WebRtcSession::Close() {
751 SetState(STATE_CLOSED);
752 RemoveUnusedChannels(nullptr);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000753 ASSERT(!voice_channel_);
754 ASSERT(!video_channel_);
755 ASSERT(!data_channel_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756}
757
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000758void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
759 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760}
761
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000762cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
763 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764}
765
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000766bool WebRtcSession::GetSslRole(rtc::SSLRole* role) {
deadbeefd59daf82015-10-14 15:02:44 -0700767 if (!local_desc_ || !remote_desc_) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000768 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
769 << "SSL Role of the session.";
770 return false;
771 }
772
deadbeefd59daf82015-10-14 15:02:44 -0700773 return transport_controller_->GetSslRole(role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000774}
775
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000776void WebRtcSession::CreateOffer(
777 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700778 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
779 const cricket::MediaSessionOptions& session_options) {
780 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000781}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782
deadbeefab9b2d12015-10-14 11:33:11 -0700783void WebRtcSession::CreateAnswer(
784 CreateSessionDescriptionObserver* observer,
785 const MediaConstraintsInterface* constraints,
786 const cricket::MediaSessionOptions& session_options) {
787 webrtc_session_desc_factory_->CreateAnswer(observer, constraints,
788 session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789}
790
791bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
792 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700793 ASSERT(signaling_thread()->IsCurrent());
794
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000795 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000796 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000797
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000798 // Validate SDP.
799 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
800 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801 }
802
deadbeefd59daf82015-10-14 15:02:44 -0700803 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000804 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700806 initial_offerer_ = true;
807 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808 }
809
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000810 cricket::SecurePolicy sdes_policy =
811 webrtc_session_desc_factory_->SdesPolicy();
812 cricket::CryptoType crypto_required = dtls_enabled_ ?
813 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
814 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000816 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000818 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819
820 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000821 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 // TODO(mallinath) - Handle CreateChannel failure, as new local description
823 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000824 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000825 }
826
deadbeefcbecd352015-09-23 11:50:27 -0700827 // Remove unused channels if MediaContentDescription is rejected.
828 RemoveUnusedChannels(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000829
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000830 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000831 return false;
832 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000833
deadbeefd59daf82015-10-14 15:02:44 -0700834 if (remote_desc_) {
835 // Now that we have a local description, we can push down remote candidates.
deadbeefcbecd352015-09-23 11:50:27 -0700836 UseCandidatesInSessionDescription(remote_desc_.get());
837 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838
deadbeefd59daf82015-10-14 15:02:44 -0700839 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000840 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841 }
842 return true;
843}
844
845bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
846 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700847 ASSERT(signaling_thread()->IsCurrent());
848
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000849 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000850 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000851
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000852 // Validate SDP.
853 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
854 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000855 }
856
deadbeefd59daf82015-10-14 15:02:44 -0700857 rtc::scoped_ptr<SessionDescriptionInterface> old_remote_desc(
858 remote_desc_.release());
859 remote_desc_.reset(desc_temp.release());
860
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000861 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000862 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 if (action == kOffer && !CreateChannels(desc->description())) {
864 // TODO(mallinath) - Handle CreateChannel failure, as new local description
865 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000866 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 }
868
deadbeefcbecd352015-09-23 11:50:27 -0700869 // Remove unused channels if MediaContentDescription is rejected.
870 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000871
872 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
873 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000874 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 return false;
876 }
877
deadbeefd59daf82015-10-14 15:02:44 -0700878 if (local_desc_ && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000879 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880 }
881
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882 // Check if this new SessionDescription contains new ice ufrag and password
883 // that indicates the remote peer requests ice restart.
honghaiz503726c2015-07-31 12:37:38 -0700884 bool ice_restart =
deadbeefd59daf82015-10-14 15:02:44 -0700885 ice_restart_latch_->CheckForRemoteIceRestart(old_remote_desc.get(), desc);
honghaiz503726c2015-07-31 12:37:38 -0700886 // We retain all received candidates only if ICE is not restarted.
887 // When ICE is restarted, all previous candidates belong to an old generation
888 // and should not be kept.
deadbeefd59daf82015-10-14 15:02:44 -0700889 // TODO(deadbeef): This goes against the W3C spec which says the remote
890 // description should only contain candidates from the last set remote
891 // description plus any candidates added since then. We should remove this
892 // once we're sure it won't break anything.
honghaiz503726c2015-07-31 12:37:38 -0700893 if (!ice_restart) {
894 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
deadbeefd59daf82015-10-14 15:02:44 -0700895 old_remote_desc.get(), desc);
honghaiz503726c2015-07-31 12:37:38 -0700896 }
897
deadbeefd59daf82015-10-14 15:02:44 -0700898 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000899 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000901
902 // Set the the ICE connection state to connecting since the connection may
903 // become writable with peer reflexive candidates before any remote candidate
904 // is signaled.
905 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
906 // is to have a new signal the indicates a change in checking state from the
907 // transport and expose a new checking() member from transport that can be
908 // read to determine the current checking state. The existing SignalConnecting
909 // actually means "gathering candidates", so cannot be be used here.
910 if (desc->type() != SessionDescriptionInterface::kOffer &&
911 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
912 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
913 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 return true;
915}
916
deadbeefd59daf82015-10-14 15:02:44 -0700917void WebRtcSession::LogState(State old_state, State new_state) {
918 LOG(LS_INFO) << "Session:" << id()
919 << " Old state:" << GetStateString(old_state)
920 << " New state:" << GetStateString(new_state);
921}
922
923void WebRtcSession::SetState(State state) {
924 ASSERT(signaling_thread_->IsCurrent());
925 if (state != state_) {
926 LogState(state_, state);
927 state_ = state;
928 SignalState(this, state_);
929 }
930}
931
932void WebRtcSession::SetError(Error error, const std::string& error_desc) {
933 ASSERT(signaling_thread_->IsCurrent());
934 if (error != error_) {
935 error_ = error;
936 error_desc_ = error_desc;
937 }
938}
939
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000940bool WebRtcSession::UpdateSessionState(
941 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700943 ASSERT(signaling_thread()->IsCurrent());
944
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945 // If there's already a pending error then no state transition should happen.
946 // But all call-sites should be verifying this before calling us!
deadbeefd59daf82015-10-14 15:02:44 -0700947 ASSERT(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000948 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000950 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
951 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000952 }
deadbeefd59daf82015-10-14 15:02:44 -0700953 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
954 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000955 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700956 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000957 }
deadbeefd59daf82015-10-14 15:02:44 -0700958 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000959 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000960 }
961 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000962 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
963 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964 }
965 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700966 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
967 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000968 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700969 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000970 }
deadbeefd59daf82015-10-14 15:02:44 -0700971 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000972 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000973 }
974 } else if (action == kAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000975 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
976 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000977 }
deadbeefcbecd352015-09-23 11:50:27 -0700978 const cricket::ContentGroup* local_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700979 local_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700980 const cricket::ContentGroup* remote_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700981 remote_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700982 if (local_bundle && remote_bundle) {
983 // The answerer decides the transport to bundle on
984 const cricket::ContentGroup* answer_bundle =
985 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
986 if (!EnableBundle(*answer_bundle)) {
987 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
988 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
989 }
990 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700992 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000993 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700994 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000995 }
deadbeefd59daf82015-10-14 15:02:44 -0700996 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000997 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998 }
999 }
1000 return true;
1001}
1002
1003WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
1004 if (type == SessionDescriptionInterface::kOffer) {
1005 return WebRtcSession::kOffer;
1006 } else if (type == SessionDescriptionInterface::kPrAnswer) {
1007 return WebRtcSession::kPrAnswer;
1008 } else if (type == SessionDescriptionInterface::kAnswer) {
1009 return WebRtcSession::kAnswer;
1010 }
1011 ASSERT(false && "unknown action type");
1012 return WebRtcSession::kOffer;
1013}
1014
deadbeefd59daf82015-10-14 15:02:44 -07001015bool WebRtcSession::PushdownMediaDescription(
1016 cricket::ContentAction action,
1017 cricket::ContentSource source,
1018 std::string* err) {
1019 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
1020 if (!ch) {
1021 return true;
1022 } else if (source == cricket::CS_LOCAL) {
1023 return ch->PushdownLocalDescription(local_desc_->description(), action,
1024 err);
1025 } else {
1026 return ch->PushdownRemoteDescription(remote_desc_->description(), action,
1027 err);
1028 }
1029 };
1030
1031 return (set_content(voice_channel()) &&
1032 set_content(video_channel()) &&
1033 set_content(data_channel()));
1034}
1035
1036bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
1037 cricket::ContentAction action,
1038 std::string* error_desc) {
1039 RTC_DCHECK(signaling_thread()->IsCurrent());
1040
1041 if (source == cricket::CS_LOCAL) {
1042 return PushdownLocalTransportDescription(local_desc_->description(), action,
1043 error_desc);
1044 }
1045 return PushdownRemoteTransportDescription(remote_desc_->description(), action,
1046 error_desc);
1047}
1048
1049bool WebRtcSession::PushdownLocalTransportDescription(
1050 const SessionDescription* sdesc,
1051 cricket::ContentAction action,
1052 std::string* err) {
1053 RTC_DCHECK(signaling_thread()->IsCurrent());
1054
1055 if (!sdesc) {
1056 return false;
1057 }
1058
1059 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1060 if (!transport_controller_->SetLocalTransportDescription(
1061 tinfo.content_name, tinfo.description, action, err)) {
1062 return false;
1063 }
1064 }
1065
1066 return true;
1067}
1068
1069bool WebRtcSession::PushdownRemoteTransportDescription(
1070 const SessionDescription* sdesc,
1071 cricket::ContentAction action,
1072 std::string* err) {
1073 RTC_DCHECK(signaling_thread()->IsCurrent());
1074
1075 if (!sdesc) {
1076 return false;
1077 }
1078
1079 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1080 if (!transport_controller_->SetRemoteTransportDescription(
1081 tinfo.content_name, tinfo.description, action, err)) {
1082 return false;
1083 }
1084 }
1085
1086 return true;
1087}
1088
1089bool WebRtcSession::GetTransportDescription(
1090 const SessionDescription* description,
1091 const std::string& content_name,
1092 cricket::TransportDescription* tdesc) {
1093 if (!description || !tdesc) {
1094 return false;
1095 }
1096 const TransportInfo* transport_info =
1097 description->GetTransportInfoByName(content_name);
1098 if (!transport_info) {
1099 return false;
1100 }
1101 *tdesc = transport_info->description;
1102 return true;
1103}
1104
1105bool WebRtcSession::GetTransportStats(SessionStats* stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001106 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001107 return (GetChannelTransportStats(voice_channel(), stats) &&
1108 GetChannelTransportStats(video_channel(), stats) &&
1109 GetChannelTransportStats(data_channel(), stats));
1110}
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001111
deadbeefcbecd352015-09-23 11:50:27 -07001112bool WebRtcSession::GetChannelTransportStats(cricket::BaseChannel* ch,
deadbeefd59daf82015-10-14 15:02:44 -07001113 SessionStats* stats) {
deadbeefcbecd352015-09-23 11:50:27 -07001114 ASSERT(signaling_thread()->IsCurrent());
1115 if (!ch) {
1116 // Not using this channel.
1117 return true;
1118 }
1119
1120 const std::string& content_name = ch->content_name();
1121 const std::string& transport_name = ch->transport_name();
1122 stats->proxy_to_transport[content_name] = transport_name;
1123 if (stats->transport_stats.find(transport_name) !=
1124 stats->transport_stats.end()) {
1125 // Transport stats already done for this transport.
1126 return true;
1127 }
1128
1129 cricket::TransportStats tstats;
deadbeefd59daf82015-10-14 15:02:44 -07001130 if (!transport_controller_->GetStats(transport_name, &tstats)) {
deadbeefcbecd352015-09-23 11:50:27 -07001131 return false;
1132 }
1133
1134 stats->transport_stats[transport_name] = tstats;
1135 return true;
1136}
1137
1138bool WebRtcSession::GetLocalCertificate(
1139 const std::string& transport_name,
1140 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1141 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001142 return transport_controller_->GetLocalCertificate(transport_name,
1143 certificate);
deadbeefcbecd352015-09-23 11:50:27 -07001144}
1145
1146bool WebRtcSession::GetRemoteSSLCertificate(const std::string& transport_name,
1147 rtc::SSLCertificate** cert) {
1148 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001149 return transport_controller_->GetRemoteSSLCertificate(transport_name, cert);
deadbeefcbecd352015-09-23 11:50:27 -07001150}
1151
1152cricket::BaseChannel* WebRtcSession::GetChannel(
1153 const std::string& content_name) {
1154 if (voice_channel() && voice_channel()->content_name() == content_name) {
1155 return voice_channel();
1156 }
1157 if (video_channel() && video_channel()->content_name() == content_name) {
1158 return video_channel();
1159 }
1160 if (data_channel() && data_channel()->content_name() == content_name) {
1161 return data_channel();
1162 }
1163 return nullptr;
1164}
1165
1166bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1167 const std::string* first_content_name = bundle.FirstContentName();
1168 if (!first_content_name) {
1169 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1170 return false;
1171 }
1172 const std::string& transport_name = *first_content_name;
1173 cricket::BaseChannel* first_channel = GetChannel(transport_name);
1174
1175 auto maybe_set_transport = [this, bundle, transport_name,
1176 first_channel](cricket::BaseChannel* ch) {
1177 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001178 return true;
1179 }
1180
deadbeefcbecd352015-09-23 11:50:27 -07001181 if (ch->transport_name() == transport_name) {
1182 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1183 << " on " << transport_name << ".";
1184 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001185 }
torbjornga81a42f2015-09-23 02:16:58 -07001186
deadbeefcbecd352015-09-23 11:50:27 -07001187 if (!ch->SetTransport(transport_name)) {
1188 LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name();
1189 return false;
1190 }
1191 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1192 << transport_name << ".";
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001193 return true;
1194 };
1195
deadbeefcbecd352015-09-23 11:50:27 -07001196 if (!maybe_set_transport(voice_channel()) ||
1197 !maybe_set_transport(video_channel()) ||
1198 !maybe_set_transport(data_channel())) {
1199 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001200 }
deadbeefcbecd352015-09-23 11:50:27 -07001201
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001202 return true;
1203}
1204
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001205bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001206 if (!remote_desc_) {
1207 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1208 << "without any remote session description.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001209 return false;
1210 }
1211
1212 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001213 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001214 return false;
1215 }
1216
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001217 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001218 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1219 if (!valid) {
1220 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001221 }
1222
1223 // Add this candidate to the remote session description.
1224 if (!remote_desc_->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001225 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001226 return false;
1227 }
1228
deadbeefd59daf82015-10-14 15:02:44 -07001229 if (ready) {
1230 return UseCandidate(candidate);
1231 } else {
1232 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1233 return true;
1234 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001235}
1236
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001237bool WebRtcSession::SetIceTransports(
1238 PeerConnectionInterface::IceTransportsType type) {
1239 return port_allocator()->set_candidate_filter(
1240 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001241}
1242
deadbeefd59daf82015-10-14 15:02:44 -07001243cricket::IceConfig WebRtcSession::ParseIceConfig(
1244 const PeerConnectionInterface::RTCConfiguration& config) const {
1245 cricket::IceConfig ice_config;
1246 ice_config.receiving_timeout_ms = config.ice_connection_receiving_timeout;
1247 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
Peter Boström0c4e06b2015-10-07 12:23:21 +02001323bool WebRtcSession::SetCaptureDevice(uint32_t ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001324 cricket::VideoCapturer* camera) {
1325 ASSERT(signaling_thread()->IsCurrent());
1326
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001327 if (!video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001328 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1329 // support video.
1330 LOG(LS_WARNING) << "Video not used in this call.";
1331 return false;
1332 }
1333 if (!video_channel_->SetCapturer(ssrc, camera)) {
1334 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1335 // This in the normal case when the underlying media channel has already
1336 // been deleted.
1337 ASSERT(camera == NULL);
1338 return false;
1339 }
1340 return true;
1341}
1342
Peter Boström0c4e06b2015-10-07 12:23:21 +02001343void WebRtcSession::SetVideoPlayout(uint32_t ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001344 bool enable,
1345 cricket::VideoRenderer* renderer) {
1346 ASSERT(signaling_thread()->IsCurrent());
1347 if (!video_channel_) {
1348 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1349 return;
1350 }
1351 if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) {
1352 // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise.
1353 // This in the normal case when the underlying media channel has already
1354 // been deleted.
1355 ASSERT(renderer == NULL);
1356 }
1357}
1358
Peter Boström0c4e06b2015-10-07 12:23:21 +02001359void WebRtcSession::SetVideoSend(uint32_t ssrc,
1360 bool enable,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001361 const cricket::VideoOptions* options) {
1362 ASSERT(signaling_thread()->IsCurrent());
1363 if (!video_channel_) {
1364 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1365 return;
1366 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001367 if (!video_channel_->SetVideoSend(ssrc, enable, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001368 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1369 // This in the normal case when the underlying media channel has already
1370 // been deleted.
1371 ASSERT(enable == false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001372 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001373}
1374
1375bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1376 ASSERT(signaling_thread()->IsCurrent());
1377 if (!voice_channel_) {
1378 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1379 return false;
1380 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001381 uint32_t send_ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001382 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1383 // exists.
deadbeefd59daf82015-10-14 15:02:44 -07001384 if (!local_desc_ ||
1385 !GetAudioSsrcByTrackId(local_desc_->description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001386 &send_ssrc)) {
1387 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1388 return false;
1389 }
1390 return voice_channel_->CanInsertDtmf();
1391}
1392
1393bool WebRtcSession::InsertDtmf(const std::string& track_id,
1394 int code, int duration) {
1395 ASSERT(signaling_thread()->IsCurrent());
1396 if (!voice_channel_) {
1397 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1398 return false;
1399 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001400 uint32_t send_ssrc = 0;
deadbeefd59daf82015-10-14 15:02:44 -07001401 if (!VERIFY(local_desc_ && GetAudioSsrcByTrackId(local_desc_->description(),
1402 track_id, &send_ssrc))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001403 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1404 return false;
1405 }
solenberg1d63dd02015-12-02 12:35:09 -08001406 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001407 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1408 return false;
1409 }
1410 return true;
1411}
1412
1413sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
1414 return &SignalVoiceChannelDestroyed;
1415}
1416
wu@webrtc.org78187522013-10-07 23:32:02 +00001417bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001418 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001419 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001420 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001421 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1422 return false;
1423 }
1424 return data_channel_->SendData(params, payload, result);
1425}
1426
1427bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001428 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001429 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1430 return false;
1431 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001432 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1433 &DataChannel::OnChannelReady);
1434 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1435 &DataChannel::OnDataReceived);
deadbeefab9b2d12015-10-14 11:33:11 -07001436 data_channel_->SignalStreamClosedRemotely.connect(
1437 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
wu@webrtc.org78187522013-10-07 23:32:02 +00001438 return true;
1439}
1440
1441void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001442 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001443 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1444 return;
1445 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001446 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1447 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
deadbeefab9b2d12015-10-14 11:33:11 -07001448 data_channel_->SignalStreamClosedRemotely.disconnect(webrtc_data_channel);
wu@webrtc.org78187522013-10-07 23:32:02 +00001449}
1450
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001451void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001452 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001453 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1454 return;
1455 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001456 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1457 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001458}
1459
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001460void WebRtcSession::RemoveSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001461 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001462 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1463 << "NULL.";
1464 return;
1465 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001466 data_channel_->RemoveRecvStream(sid);
1467 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001468}
1469
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001470bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001471 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001472}
1473
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001474cricket::DataChannelType WebRtcSession::data_channel_type() const {
1475 return data_channel_type_;
1476}
1477
wu@webrtc.org91053e72013-08-10 07:18:04 +00001478bool WebRtcSession::IceRestartPending() const {
1479 return ice_restart_latch_->Get();
1480}
1481
1482void WebRtcSession::ResetIceRestartLatch() {
1483 ice_restart_latch_->Reset();
1484}
1485
Henrik Boströmd8281982015-08-27 10:12:24 +02001486void WebRtcSession::OnCertificateReady(
1487 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001488 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001489}
1490
Henrik Boströmd8281982015-08-27 10:12:24 +02001491bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001492 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001493}
1494
deadbeefcbecd352015-09-23 11:50:27 -07001495const rtc::scoped_refptr<rtc::RTCCertificate>&
1496WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001497 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001498}
1499
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001500void WebRtcSession::SetIceConnectionState(
1501 PeerConnectionInterface::IceConnectionState state) {
1502 if (ice_connection_state_ == state) {
1503 return;
1504 }
1505
1506 // ASSERT that the requested transition is allowed. Note that
1507 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1508 // within PeerConnection). This switch statement should compile away when
1509 // ASSERTs are disabled.
deadbeefcbecd352015-09-23 11:50:27 -07001510 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1511 << " => " << state;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001512 switch (ice_connection_state_) {
1513 case PeerConnectionInterface::kIceConnectionNew:
1514 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1515 break;
1516 case PeerConnectionInterface::kIceConnectionChecking:
1517 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1518 state == PeerConnectionInterface::kIceConnectionConnected);
1519 break;
1520 case PeerConnectionInterface::kIceConnectionConnected:
1521 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1522 state == PeerConnectionInterface::kIceConnectionChecking ||
1523 state == PeerConnectionInterface::kIceConnectionCompleted);
1524 break;
1525 case PeerConnectionInterface::kIceConnectionCompleted:
1526 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1527 state == PeerConnectionInterface::kIceConnectionDisconnected);
1528 break;
1529 case PeerConnectionInterface::kIceConnectionFailed:
1530 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1531 break;
1532 case PeerConnectionInterface::kIceConnectionDisconnected:
1533 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1534 state == PeerConnectionInterface::kIceConnectionConnected ||
1535 state == PeerConnectionInterface::kIceConnectionCompleted ||
1536 state == PeerConnectionInterface::kIceConnectionFailed);
1537 break;
1538 case PeerConnectionInterface::kIceConnectionClosed:
1539 ASSERT(false);
1540 break;
1541 default:
1542 ASSERT(false);
1543 break;
1544 }
1545
1546 ice_connection_state_ = state;
1547 if (ice_observer_) {
1548 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1549 }
1550}
1551
deadbeefcbecd352015-09-23 11:50:27 -07001552void WebRtcSession::OnTransportControllerConnectionState(
1553 cricket::IceConnectionState state) {
1554 switch (state) {
1555 case cricket::kIceConnectionConnecting:
1556 // If the current state is Connected or Completed, then there were
1557 // writable channels but now there are not, so the next state must
1558 // be Disconnected.
1559 // kIceConnectionConnecting is currently used as the default,
1560 // un-connected state by the TransportController, so its only use is
1561 // detecting disconnections.
1562 if (ice_connection_state_ ==
1563 PeerConnectionInterface::kIceConnectionConnected ||
1564 ice_connection_state_ ==
1565 PeerConnectionInterface::kIceConnectionCompleted) {
1566 SetIceConnectionState(
1567 PeerConnectionInterface::kIceConnectionDisconnected);
1568 }
torbjornga81a42f2015-09-23 02:16:58 -07001569 break;
deadbeefcbecd352015-09-23 11:50:27 -07001570 case cricket::kIceConnectionFailed:
1571 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1572 break;
1573 case cricket::kIceConnectionConnected:
1574 LOG(LS_INFO) << "Changing to ICE connected state because "
1575 << "all transports are writable.";
1576 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1577 break;
1578 case cricket::kIceConnectionCompleted:
1579 LOG(LS_INFO) << "Changing to ICE completed state because "
1580 << "all transports are complete.";
1581 if (ice_connection_state_ !=
1582 PeerConnectionInterface::kIceConnectionConnected) {
1583 // If jumping directly from "checking" to "connected",
1584 // signal "connected" first.
1585 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1586 }
1587 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1588 if (metrics_observer_) {
1589 ReportTransportStats();
1590 }
1591 break;
1592 default:
1593 ASSERT(false);
torbjornga81a42f2015-09-23 02:16:58 -07001594 }
deadbeefcbecd352015-09-23 11:50:27 -07001595}
1596
1597void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001598 SetIceConnectionReceiving(receiving);
1599}
1600
1601void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1602 if (ice_connection_receiving_ == receiving) {
1603 return;
1604 }
1605 ice_connection_receiving_ = receiving;
1606 if (ice_observer_) {
1607 ice_observer_->OnIceConnectionReceivingChange(receiving);
1608 }
1609}
1610
deadbeefcbecd352015-09-23 11:50:27 -07001611void WebRtcSession::OnTransportControllerCandidatesGathered(
1612 const std::string& transport_name,
1613 const cricket::Candidates& candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001614 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001615 int sdp_mline_index;
1616 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1617 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1618 << transport_name << " not found";
1619 return;
1620 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001621
deadbeefcbecd352015-09-23 11:50:27 -07001622 for (cricket::Candidates::const_iterator citer = candidates.begin();
1623 citer != candidates.end(); ++citer) {
1624 // Use transport_name as the candidate media id.
1625 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1626 if (ice_observer_) {
1627 ice_observer_->OnIceCandidate(&candidate);
1628 }
1629 if (local_desc_) {
1630 local_desc_->AddCandidate(&candidate);
1631 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001632 }
1633}
1634
1635// Enabling voice and video channel.
1636void WebRtcSession::EnableChannels() {
1637 if (voice_channel_ && !voice_channel_->enabled())
1638 voice_channel_->Enable(true);
1639
1640 if (video_channel_ && !video_channel_->enabled())
1641 video_channel_->Enable(true);
1642
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001643 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001644 data_channel_->Enable(true);
1645}
1646
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001647// Returns the media index for a local ice candidate given the content name.
1648bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1649 int* sdp_mline_index) {
deadbeefd59daf82015-10-14 15:02:44 -07001650 if (!local_desc_ || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001651 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001652 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001653
1654 bool content_found = false;
deadbeefd59daf82015-10-14 15:02:44 -07001655 const ContentInfos& contents = local_desc_->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001656 for (size_t index = 0; index < contents.size(); ++index) {
1657 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001658 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001659 content_found = true;
1660 break;
1661 }
1662 }
1663 return content_found;
1664}
1665
1666bool WebRtcSession::UseCandidatesInSessionDescription(
1667 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001668 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001670 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001671 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001672
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001673 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1674 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001675 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001676 const IceCandidateInterface* candidate = candidates->at(n);
1677 bool valid = false;
1678 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1679 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001680 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1681 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001682 }
1683 continue;
1684 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001685 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001686 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001687 break;
deadbeefd59daf82015-10-14 15:02:44 -07001688 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001689 }
1690 }
1691 return ret;
1692}
1693
1694bool WebRtcSession::UseCandidate(
1695 const IceCandidateInterface* candidate) {
1696
1697 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeefd59daf82015-10-14 15:02:44 -07001698 size_t remote_content_size = remote_desc_->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001699 if (mediacontent_index >= remote_content_size) {
1700 LOG(LS_ERROR)
1701 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1702 return false;
1703 }
1704
1705 cricket::ContentInfo content =
deadbeefd59daf82015-10-14 15:02:44 -07001706 remote_desc_->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001707 std::vector<cricket::Candidate> candidates;
1708 candidates.push_back(candidate->candidate());
1709 // Invoking BaseSession method to handle remote candidates.
1710 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001711 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1712 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713 // Candidates successfully submitted for checking.
1714 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1715 ice_connection_state_ ==
1716 PeerConnectionInterface::kIceConnectionDisconnected) {
1717 // If state is New, then the session has just gotten its first remote ICE
1718 // candidates, so go to Checking.
1719 // If state is Disconnected, the session is re-using old candidates or
1720 // receiving additional ones, so go to Checking.
1721 // If state is Connected, stay Connected.
1722 // TODO(bemasc): If state is Connected, and the new candidates are for a
1723 // newly added transport, then the state actually _should_ move to
1724 // checking. Add a way to distinguish that case.
1725 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1726 }
1727 // TODO(bemasc): If state is Completed, go back to Connected.
1728 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001729 if (!error.empty()) {
1730 LOG(LS_WARNING) << error;
1731 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732 }
1733 return true;
1734}
1735
deadbeefcbecd352015-09-23 11:50:27 -07001736void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001737 // Destroy video_channel_ first since it may have a pointer to the
1738 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001739 const cricket::ContentInfo* video_info =
1740 cricket::GetFirstVideoContent(desc);
1741 if ((!video_info || video_info->rejected) && video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001742 SignalVideoChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001743 channel_manager_->DestroyVideoChannel(video_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001744 }
1745
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001746 const cricket::ContentInfo* voice_info =
1747 cricket::GetFirstAudioContent(desc);
1748 if ((!voice_info || voice_info->rejected) && voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001749 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001750 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001751 }
1752
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001753 const cricket::ContentInfo* data_info =
1754 cricket::GetFirstDataContent(desc);
1755 if ((!data_info || data_info->rejected) && data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001756 SignalDataChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001757 channel_manager_->DestroyDataChannel(data_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001758 }
1759}
1760
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001761// TODO(mallinath) - Add a correct error code if the channels are not created
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001762// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001763bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001764 // Creating the media channels and transport proxies.
1765 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1766 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001767 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768 LOG(LS_ERROR) << "Failed to create voice channel.";
1769 return false;
1770 }
1771 }
1772
1773 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1774 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001775 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001776 LOG(LS_ERROR) << "Failed to create video channel.";
1777 return false;
1778 }
1779 }
1780
1781 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1782 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001783 data && !data->rejected && !data_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001784 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785 LOG(LS_ERROR) << "Failed to create data channel.";
1786 return false;
1787 }
1788 }
1789
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001790 if (rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire) {
1791 if (voice_channel()) {
1792 voice_channel()->ActivateRtcpMux();
1793 }
1794 if (video_channel()) {
1795 video_channel()->ActivateRtcpMux();
1796 }
1797 if (data_channel()) {
1798 data_channel()->ActivateRtcpMux();
1799 }
1800 }
1801
deadbeefcbecd352015-09-23 11:50:27 -07001802 // Enable BUNDLE immediately when kBundlePolicyMaxBundle is in effect.
Donald Curtis0e209b02015-03-24 09:29:54 -07001803 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
Peter Thatcher4eddf182015-04-30 10:55:59 -07001804 const cricket::ContentGroup* bundle_group = desc->GetGroupByName(
1805 cricket::GROUP_TYPE_BUNDLE);
1806 if (!bundle_group) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001807 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1808 return false;
1809 }
deadbeefcbecd352015-09-23 11:50:27 -07001810 if (!EnableBundle(*bundle_group)) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001811 LOG(LS_WARNING) << "max-bundle failed to enable bundling.";
1812 return false;
1813 }
1814 }
1815
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001816 return true;
1817}
1818
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001819bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001820 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001821 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001822 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001823 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001824 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001825 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001826
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001827 voice_channel_->SignalDtlsSetupFailure.connect(
1828 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001829
1830 SignalVoiceChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001831 voice_channel_->transport_channel()->SignalSentPacket.connect(
1832 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001833 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001834}
1835
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001836bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001837 video_channel_.reset(channel_manager_->CreateVideoChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001838 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001839 video_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001840 if (!video_channel_) {
1841 return false;
1842 }
1843
1844 video_channel_->SignalDtlsSetupFailure.connect(
1845 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001846
1847 SignalVideoChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001848 video_channel_->transport_channel()->SignalSentPacket.connect(
1849 this, &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001850 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001851}
1852
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001853bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001854 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001855 data_channel_.reset(channel_manager_->CreateDataChannel(
deadbeefd59daf82015-10-14 15:02:44 -07001856 transport_controller_.get(), content->name, !sctp, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001857 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001858 return false;
1859 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001860
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001861 if (sctp) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001862 data_channel_->SignalDataReceived.connect(
1863 this, &WebRtcSession::OnDataChannelMessageReceived);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001864 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001865
1866 data_channel_->SignalDtlsSetupFailure.connect(
1867 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001868
1869 SignalDataChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001870 data_channel_->transport_channel()->SignalSentPacket.connect(
1871 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001872 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873}
1874
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001875void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
deadbeefd59daf82015-10-14 15:02:44 -07001876 SetError(ERROR_TRANSPORT,
1877 rtcp ? kDtlsSetupFailureRtcp : kDtlsSetupFailureRtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001878}
1879
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001880void WebRtcSession::OnDataChannelMessageReceived(
1881 cricket::DataChannel* channel,
1882 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001883 const rtc::Buffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001884 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1885 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
1886 // Received OPEN message; parse and signal that a new data channel should
1887 // be created.
1888 std::string label;
1889 InternalDataChannelInit config;
1890 config.id = params.ssrc;
1891 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
1892 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
1893 << params.ssrc;
1894 return;
1895 }
1896 config.open_handshake_role = InternalDataChannelInit::kAcker;
1897 SignalDataChannelOpenMessage(label, config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001898 }
deadbeefab9b2d12015-10-14 11:33:11 -07001899 // Otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001900}
1901
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001902// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001903bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001904 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1905 if (!bundle_enabled)
1906 return true;
1907
1908 const cricket::ContentGroup* bundle_group =
1909 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1910 ASSERT(bundle_group != NULL);
1911
1912 const cricket::ContentInfos& contents = desc->contents();
1913 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1914 citer != contents.end(); ++citer) {
1915 const cricket::ContentInfo* content = (&*citer);
1916 ASSERT(content != NULL);
1917 if (bundle_group->HasContentName(content->name) &&
1918 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1919 if (!HasRtcpMuxEnabled(content))
1920 return false;
1921 }
1922 }
1923 // RTCP-MUX is enabled in all the contents.
1924 return true;
1925}
1926
1927bool WebRtcSession::HasRtcpMuxEnabled(
1928 const cricket::ContentInfo* content) {
1929 const cricket::MediaContentDescription* description =
1930 static_cast<cricket::MediaContentDescription*>(content->description);
1931 return description->rtcp_mux();
1932}
1933
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001934bool WebRtcSession::ValidateSessionDescription(
1935 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001936 cricket::ContentSource source, std::string* err_desc) {
1937 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07001938 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001939 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001940 }
1941
1942 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001943 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001944 }
1945
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001946 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001947 Action action = GetAction(sdesc->type());
1948 if (source == cricket::CS_LOCAL) {
1949 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001950 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001951 } else {
1952 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001953 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001954 }
1955
1956 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001957 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001958 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1959 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001960 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001961 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001962 }
1963
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001964 // Verify ice-ufrag and ice-pwd.
1965 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001966 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001967 }
1968
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001969 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001970 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001971 }
1972
1973 // Verify m-lines in Answer when compared against Offer.
1974 if (action == kAnswer) {
1975 const cricket::SessionDescription* offer_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001976 (source == cricket::CS_LOCAL) ? remote_desc_->description()
1977 : local_desc_->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001978 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001979 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001980 }
1981 }
1982
1983 return true;
1984}
1985
1986bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1987 return ((action == kOffer && state() == STATE_INIT) ||
1988 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07001989 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001990 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001991 (action == kOffer && state() == STATE_INPROGRESS) ||
1992 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001993 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
1994 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
1995 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
1996 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001997}
1998
1999bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
2000 return ((action == kOffer && state() == STATE_INIT) ||
2001 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002002 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002003 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002004 (action == kOffer && state() == STATE_INPROGRESS) ||
2005 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07002006 (action == kAnswer && state() == STATE_SENTOFFER) ||
2007 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
2008 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
2009 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002010}
2011
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002012std::string WebRtcSession::GetSessionErrorMsg() {
2013 std::ostringstream desc;
2014 desc << kSessionError << GetErrorCodeString(error()) << ". ";
2015 desc << kSessionErrorDesc << error_desc() << ".";
2016 return desc.str();
2017}
2018
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002019// We need to check the local/remote description for the Transport instead of
2020// the session, because a new Transport added during renegotiation may have
2021// them unset while the session has them set from the previous negotiation.
2022// Not doing so may trigger the auto generation of transport description and
2023// mess up DTLS identity information, ICE credential, etc.
2024bool WebRtcSession::ReadyToUseRemoteCandidate(
2025 const IceCandidateInterface* candidate,
2026 const SessionDescriptionInterface* remote_desc,
2027 bool* valid) {
2028 *valid = true;;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002029
2030 const SessionDescriptionInterface* current_remote_desc =
deadbeefd59daf82015-10-14 15:02:44 -07002031 remote_desc ? remote_desc : remote_desc_.get();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002032
deadbeefd59daf82015-10-14 15:02:44 -07002033 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002034 return false;
deadbeefd59daf82015-10-14 15:02:44 -07002035 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002036
2037 size_t mediacontent_index =
2038 static_cast<size_t>(candidate->sdp_mline_index());
2039 size_t remote_content_size =
2040 current_remote_desc->description()->contents().size();
2041 if (mediacontent_index >= remote_content_size) {
2042 LOG(LS_ERROR)
2043 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
2044
2045 *valid = false;
2046 return false;
2047 }
2048
2049 cricket::ContentInfo content =
2050 current_remote_desc->description()->contents()[mediacontent_index];
deadbeefcbecd352015-09-23 11:50:27 -07002051 cricket::BaseChannel* channel = GetChannel(content.name);
2052 if (!channel) {
2053 return false;
2054 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002055
deadbeefd59daf82015-10-14 15:02:44 -07002056 return transport_controller_->ReadyForRemoteCandidates(
deadbeefcbecd352015-09-23 11:50:27 -07002057 channel->transport_name());
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002058}
2059
deadbeefcbecd352015-09-23 11:50:27 -07002060void WebRtcSession::OnTransportControllerGatheringState(
2061 cricket::IceGatheringState state) {
2062 ASSERT(signaling_thread()->IsCurrent());
2063 if (state == cricket::kIceGatheringGathering) {
2064 if (ice_observer_) {
2065 ice_observer_->OnIceGatheringChange(
2066 PeerConnectionInterface::kIceGatheringGathering);
2067 }
2068 } else if (state == cricket::kIceGatheringComplete) {
2069 if (ice_observer_) {
2070 ice_observer_->OnIceGatheringChange(
2071 PeerConnectionInterface::kIceGatheringComplete);
2072 ice_observer_->OnIceComplete();
2073 }
2074 }
2075}
2076
2077void WebRtcSession::ReportTransportStats() {
2078 // Use a set so we don't report the same stats twice if two channels share
2079 // a transport.
2080 std::set<std::string> transport_names;
2081 if (voice_channel()) {
2082 transport_names.insert(voice_channel()->transport_name());
2083 }
2084 if (video_channel()) {
2085 transport_names.insert(video_channel()->transport_name());
2086 }
2087 if (data_channel()) {
2088 transport_names.insert(data_channel()->transport_name());
2089 }
2090 for (const auto& name : transport_names) {
2091 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002092 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002093 ReportBestConnectionState(stats);
2094 ReportNegotiatedCiphers(stats);
2095 }
2096 }
2097}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002098// Walk through the ConnectionInfos to gather best connection usage
2099// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002100void WebRtcSession::ReportBestConnectionState(
2101 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002102 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002103 for (cricket::TransportChannelStatsList::const_iterator it =
2104 stats.channel_stats.begin();
2105 it != stats.channel_stats.end(); ++it) {
2106 for (cricket::ConnectionInfos::const_iterator it_info =
2107 it->connection_infos.begin();
2108 it_info != it->connection_infos.end(); ++it_info) {
2109 if (!it_info->best_connection) {
2110 continue;
2111 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002112
2113 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2114 const cricket::Candidate& local = it_info->local_candidate;
2115 const cricket::Candidate& remote = it_info->remote_candidate;
2116
2117 // Increment the counter for IceCandidatePairType.
2118 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2119 (local.type() == RELAY_PORT_TYPE &&
2120 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2121 type = kEnumCounterIceCandidatePairTypeTcp;
2122 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2123 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002124 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002125 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002126 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002127 metrics_observer_->IncrementEnumCounter(
2128 type, GetIceCandidatePairCounter(local, remote),
2129 kIceCandidatePairMax);
2130
2131 // Increment the counter for IP type.
2132 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002133 metrics_observer_->IncrementEnumCounter(
2134 kEnumCounterAddressFamily, kBestConnections_IPv4,
2135 kPeerConnectionAddressFamilyCounter_Max);
2136
2137 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002138 metrics_observer_->IncrementEnumCounter(
2139 kEnumCounterAddressFamily, kBestConnections_IPv6,
2140 kPeerConnectionAddressFamilyCounter_Max);
2141 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002142 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002143 }
2144
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002145 return;
2146 }
2147 }
2148}
2149
jbauchac8869e2015-07-03 01:36:14 -07002150void WebRtcSession::ReportNegotiatedCiphers(
2151 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002152 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002153 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2154 return;
2155 }
2156
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002157 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2158 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2159 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2160 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
jbauchac8869e2015-07-03 01:36:14 -07002161 return;
2162 }
2163
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002164 PeerConnectionEnumCounterType srtp_counter_type;
2165 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002166 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002167 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2168 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002169 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002170 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2171 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002172 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002173 srtp_counter_type = kEnumCounterDataSrtpCipher;
2174 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002175 } else {
2176 RTC_NOTREACHED();
2177 return;
2178 }
2179
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002180 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2181 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2182 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002183 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002184 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2185 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2186 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002187 }
2188}
2189
stefanc1aeaf02015-10-15 07:26:07 -07002190void WebRtcSession::OnSentPacket_w(cricket::TransportChannel* channel,
2191 const rtc::SentPacket& sent_packet) {
2192 RTC_DCHECK(worker_thread()->IsCurrent());
2193 media_controller_->call_w()->OnSentPacket(sent_packet);
2194}
2195
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002196} // namespace webrtc