blob: 15feb539ceb23b53f07d0b95b9d770dd52fad9d7 [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
Henrik Kjellander15583c12016-02-10 10:53:12 +010028#include "webrtc/api/webrtcsession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029
pbos@webrtc.org371243d2014-03-07 15:22:04 +000030#include <limits.h>
31
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032#include <algorithm>
deadbeefcbecd352015-09-23 11:50:27 -070033#include <set>
Tommif888bb52015-12-12 01:37:01 +010034#include <utility>
35#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037#include "talk/session/media/channel.h"
38#include "talk/session/media/channelmanager.h"
39#include "talk/session/media/mediasession.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010040#include "webrtc/api/jsepicecandidate.h"
41#include "webrtc/api/jsepsessiondescription.h"
42#include "webrtc/api/mediaconstraintsinterface.h"
43#include "webrtc/api/peerconnectioninterface.h"
44#include "webrtc/api/sctputils.h"
45#include "webrtc/api/webrtcsessiondescriptionfactory.h"
Tommif888bb52015-12-12 01:37:01 +010046#include "webrtc/audio/audio_sink.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"
kjellandera96e2d72016-02-04 23:52:28 -080054#include "webrtc/media/base/constants.h"
55#include "webrtc/media/base/videocapturer.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000056#include "webrtc/p2p/base/portallocator.h"
stefanc1aeaf02015-10-15 07:26:07 -070057#include "webrtc/p2p/base/transportchannel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058
59using cricket::ContentInfo;
60using cricket::ContentInfos;
61using cricket::MediaContentDescription;
62using cricket::SessionDescription;
63using cricket::TransportInfo;
64
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070065using cricket::LOCAL_PORT_TYPE;
66using cricket::STUN_PORT_TYPE;
67using cricket::RELAY_PORT_TYPE;
68using cricket::PRFLX_PORT_TYPE;
69
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070namespace webrtc {
71
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000073const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
74 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000075const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076const char kInvalidCandidates[] = "Description contains invalid candidates.";
77const char kInvalidSdp[] = "Invalid session description.";
78const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000079 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
80const char kPushDownTDFailed[] =
81 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000082const char kSdpWithoutDtlsFingerprint[] =
83 "Called with SDP without DTLS fingerprint.";
84const char kSdpWithoutSdesCrypto[] =
85 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000086const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000087 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000089const char kSessionErrorDesc[] = "Session error description: ";
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000090const char kDtlsSetupFailureRtp[] =
91 "Couldn't set up DTLS-SRTP on RTP channel.";
92const char kDtlsSetupFailureRtcp[] =
93 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeefcbecd352015-09-23 11:50:27 -070094const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070096IceCandidatePairType GetIceCandidatePairCounter(
97 const cricket::Candidate& local,
98 const cricket::Candidate& remote) {
99 const auto& l = local.type();
100 const auto& r = remote.type();
101 const auto& host = LOCAL_PORT_TYPE;
102 const auto& srflx = STUN_PORT_TYPE;
103 const auto& relay = RELAY_PORT_TYPE;
104 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -0700105 if (l == host && r == host) {
106 bool local_private = IPIsPrivate(local.address().ipaddr());
107 bool remote_private = IPIsPrivate(remote.address().ipaddr());
108 if (local_private) {
109 if (remote_private) {
110 return kIceCandidatePairHostPrivateHostPrivate;
111 } else {
112 return kIceCandidatePairHostPrivateHostPublic;
113 }
114 } else {
115 if (remote_private) {
116 return kIceCandidatePairHostPublicHostPrivate;
117 } else {
118 return kIceCandidatePairHostPublicHostPublic;
119 }
120 }
121 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700122 if (l == host && r == srflx)
123 return kIceCandidatePairHostSrflx;
124 if (l == host && r == relay)
125 return kIceCandidatePairHostRelay;
126 if (l == host && r == prflx)
127 return kIceCandidatePairHostPrflx;
128 if (l == srflx && r == host)
129 return kIceCandidatePairSrflxHost;
130 if (l == srflx && r == srflx)
131 return kIceCandidatePairSrflxSrflx;
132 if (l == srflx && r == relay)
133 return kIceCandidatePairSrflxRelay;
134 if (l == srflx && r == prflx)
135 return kIceCandidatePairSrflxPrflx;
136 if (l == relay && r == host)
137 return kIceCandidatePairRelayHost;
138 if (l == relay && r == srflx)
139 return kIceCandidatePairRelaySrflx;
140 if (l == relay && r == relay)
141 return kIceCandidatePairRelayRelay;
142 if (l == relay && r == prflx)
143 return kIceCandidatePairRelayPrflx;
144 if (l == prflx && r == host)
145 return kIceCandidatePairPrflxHost;
146 if (l == prflx && r == srflx)
147 return kIceCandidatePairPrflxSrflx;
148 if (l == prflx && r == relay)
149 return kIceCandidatePairPrflxRelay;
150 return kIceCandidatePairMax;
151}
152
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153// Compares |answer| against |offer|. Comparision is done
154// for number of m-lines in answer against offer. If matches true will be
155// returned otherwise false.
156static bool VerifyMediaDescriptions(
157 const SessionDescription* answer, const SessionDescription* offer) {
158 if (offer->contents().size() != answer->contents().size())
159 return false;
160
161 for (size_t i = 0; i < offer->contents().size(); ++i) {
162 if ((offer->contents()[i].name) != answer->contents()[i].name) {
163 return false;
164 }
wu@webrtc.org4e393072014-04-07 17:04:35 +0000165 const MediaContentDescription* offer_mdesc =
166 static_cast<const MediaContentDescription*>(
167 offer->contents()[i].description);
168 const MediaContentDescription* answer_mdesc =
169 static_cast<const MediaContentDescription*>(
170 answer->contents()[i].description);
171 if (offer_mdesc->type() != answer_mdesc->type()) {
172 return false;
173 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174 }
175 return true;
176}
177
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178// Checks that each non-rejected content has SDES crypto keys or a DTLS
179// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
180// keys, will be caught in Transport negotiation, and backstopped by Channel's
181// |secure_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000182static bool VerifyCrypto(const SessionDescription* desc,
183 bool dtls_enabled,
184 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 const ContentInfos& contents = desc->contents();
186 for (size_t index = 0; index < contents.size(); ++index) {
187 const ContentInfo* cinfo = &contents[index];
188 if (cinfo->rejected) {
189 continue;
190 }
191
192 // If the content isn't rejected, crypto must be present.
193 const MediaContentDescription* media =
194 static_cast<const MediaContentDescription*>(cinfo->description);
195 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
196 if (!media || !tinfo) {
197 // Something is not right.
198 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000199 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200 return false;
201 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000202 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000203 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000204 LOG(LS_WARNING) <<
205 "Session description must have DTLS fingerprint if DTLS enabled.";
206 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000207 return false;
208 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000209 } else {
210 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000211 LOG(LS_WARNING) <<
212 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000213 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000214 return false;
215 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216 }
217 }
218
219 return true;
220}
221
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000222// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
223static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
224 const ContentInfos& contents = desc->contents();
225 for (size_t index = 0; index < contents.size(); ++index) {
226 const ContentInfo* cinfo = &contents[index];
227 if (cinfo->rejected) {
228 continue;
229 }
230
231 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
232 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
233 if (!tinfo) {
234 // Something is not right.
235 LOG(LS_ERROR) << kInvalidSdp;
236 return false;
237 }
238 if (tinfo->description.ice_ufrag.empty() ||
239 tinfo->description.ice_pwd.empty()) {
240 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
241 return false;
242 }
243 }
244 return true;
245}
246
wu@webrtc.org91053e72013-08-10 07:18:04 +0000247// Forces |sdesc->crypto_required| to the appropriate state based on the
248// current security policy, to ensure a failure occurs if there is an error
249// in crypto negotiation.
250// Called when processing the local session description.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000251static void UpdateSessionDescriptionSecurePolicy(cricket::CryptoType type,
252 SessionDescription* sdesc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000253 if (!sdesc) {
254 return;
255 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256
wu@webrtc.org91053e72013-08-10 07:18:04 +0000257 // Updating the |crypto_required_| in MediaContentDescription to the
258 // appropriate state based on the current security policy.
259 for (cricket::ContentInfos::iterator iter = sdesc->contents().begin();
260 iter != sdesc->contents().end(); ++iter) {
261 if (cricket::IsMediaContent(&*iter)) {
262 MediaContentDescription* mdesc =
263 static_cast<MediaContentDescription*> (iter->description);
264 if (mdesc) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000265 mdesc->set_crypto_required(type);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000266 }
267 }
268 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269}
270
Peter Boström0c4e06b2015-10-07 12:23:21 +0200271static bool GetAudioSsrcByTrackId(const SessionDescription* session_description,
272 const std::string& track_id,
273 uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 const cricket::ContentInfo* audio_info =
275 cricket::GetFirstAudioContent(session_description);
276 if (!audio_info) {
277 LOG(LS_ERROR) << "Audio not used in this call";
278 return false;
279 }
280
281 const cricket::MediaContentDescription* audio_content =
282 static_cast<const cricket::MediaContentDescription*>(
283 audio_info->description);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000284 const cricket::StreamParams* stream =
285 cricket::GetStreamByIds(audio_content->streams(), "", track_id);
286 if (!stream) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287 return false;
288 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000289
290 *ssrc = stream->first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 return true;
292}
293
294static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200295 uint32_t ssrc,
296 std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297 ASSERT(track_id != NULL);
298
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299 const cricket::ContentInfo* audio_info =
300 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000301 if (audio_info) {
302 const cricket::MediaContentDescription* audio_content =
303 static_cast<const cricket::MediaContentDescription*>(
304 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000306 const auto* found =
307 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
308 if (found) {
309 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000310 return true;
311 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312 }
313
314 const cricket::ContentInfo* video_info =
315 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000316 if (video_info) {
317 const cricket::MediaContentDescription* video_content =
318 static_cast<const cricket::MediaContentDescription*>(
319 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000321 const auto* found =
322 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
323 if (found) {
324 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000325 return true;
326 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327 }
328 return false;
329}
330
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000331static bool BadSdp(const std::string& source,
332 const std::string& type,
333 const std::string& reason,
334 std::string* err_desc) {
335 std::ostringstream desc;
deadbeefd59daf82015-10-14 15:02:44 -0700336 desc << "Failed to set " << source;
337 if (!type.empty()) {
338 desc << " " << type;
339 }
340 desc << " sdp: " << reason;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000341
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000343 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000345 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346 return false;
347}
348
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000350 const std::string& type,
351 const std::string& reason,
352 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000354 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000356 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357 }
358}
359
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000360static bool BadLocalSdp(const std::string& type,
361 const std::string& reason,
362 std::string* err_desc) {
363 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
364}
365
366static bool BadRemoteSdp(const std::string& type,
367 const std::string& reason,
368 std::string* err_desc) {
369 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
370}
371
372static bool BadOfferSdp(cricket::ContentSource source,
373 const std::string& reason,
374 std::string* err_desc) {
375 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
376}
377
378static bool BadPranswerSdp(cricket::ContentSource source,
379 const std::string& reason,
380 std::string* err_desc) {
381 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
382 reason, err_desc);
383}
384
385static bool BadAnswerSdp(cricket::ContentSource source,
386 const std::string& reason,
387 std::string* err_desc) {
388 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389}
390
deadbeefd59daf82015-10-14 15:02:44 -0700391#define GET_STRING_OF_STATE(state) \
392 case webrtc::WebRtcSession::state: \
393 result = #state; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000394 break;
395
deadbeefd59daf82015-10-14 15:02:44 -0700396static std::string GetStateString(webrtc::WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397 std::string result;
398 switch (state) {
399 GET_STRING_OF_STATE(STATE_INIT)
deadbeefd59daf82015-10-14 15:02:44 -0700400 GET_STRING_OF_STATE(STATE_SENTOFFER)
401 GET_STRING_OF_STATE(STATE_RECEIVEDOFFER)
402 GET_STRING_OF_STATE(STATE_SENTPRANSWER)
403 GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404 GET_STRING_OF_STATE(STATE_INPROGRESS)
deadbeefd59daf82015-10-14 15:02:44 -0700405 GET_STRING_OF_STATE(STATE_CLOSED)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406 default:
407 ASSERT(false);
408 break;
409 }
410 return result;
411}
412
deadbeefd59daf82015-10-14 15:02:44 -0700413#define GET_STRING_OF_ERROR_CODE(err) \
414 case webrtc::WebRtcSession::err: \
415 result = #err; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416 break;
417
deadbeefd59daf82015-10-14 15:02:44 -0700418static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419 std::string result;
420 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000421 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000422 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
423 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000424 default:
deadbeefd59daf82015-10-14 15:02:44 -0700425 RTC_DCHECK(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000426 break;
427 }
428 return result;
429}
430
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000431static std::string MakeErrorString(const std::string& error,
432 const std::string& desc) {
433 std::ostringstream ret;
434 ret << error << " " << desc;
435 return ret.str();
436}
437
438static std::string MakeTdErrorString(const std::string& desc) {
439 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000440}
441
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000442// Set |option| to the highest-priority value of |key| in the optional
443// constraints if the key is found and has a valid value.
kwiberg102c6a62015-10-30 02:47:38 -0700444template <typename T>
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000445static void SetOptionFromOptionalConstraint(
446 const MediaConstraintsInterface* constraints,
kwiberg102c6a62015-10-30 02:47:38 -0700447 const std::string& key,
Karl Wibergbe579832015-11-10 22:34:18 +0100448 rtc::Optional<T>* option) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000449 if (!constraints) {
450 return;
451 }
452 std::string string_value;
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000453 T value;
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000454 if (constraints->GetOptional().FindFirst(key, &string_value)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000455 if (rtc::FromString(string_value, &value)) {
Karl Wibergbe579832015-11-10 22:34:18 +0100456 *option = rtc::Optional<T>(value);
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000457 }
458 }
459}
460
Peter Boström0c4e06b2015-10-07 12:23:21 +0200461uint32_t ConvertIceTransportTypeToCandidateFilter(
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000462 PeerConnectionInterface::IceTransportsType type) {
463 switch (type) {
464 case PeerConnectionInterface::kNone:
465 return cricket::CF_NONE;
466 case PeerConnectionInterface::kRelay:
467 return cricket::CF_RELAY;
468 case PeerConnectionInterface::kNoHost:
469 return (cricket::CF_ALL & ~cricket::CF_HOST);
470 case PeerConnectionInterface::kAll:
471 return cricket::CF_ALL;
472 default: ASSERT(false);
473 }
474 return cricket::CF_NONE;
475}
476
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477// Help class used to remember if a a remote peer has requested ice restart by
478// by sending a description with new ice ufrag and password.
479class IceRestartAnswerLatch {
480 public:
481 IceRestartAnswerLatch() : ice_restart_(false) { }
482
wu@webrtc.org91053e72013-08-10 07:18:04 +0000483 // Returns true if CheckForRemoteIceRestart has been called with a new session
484 // description where ice password and ufrag has changed since last time
485 // Reset() was called.
486 bool Get() const {
487 return ice_restart_;
488 }
489
490 void Reset() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491 if (ice_restart_) {
492 ice_restart_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494 }
495
Honghai Zhang04e91462015-12-11 14:26:22 -0800496 // This method has two purposes: 1. Return whether |new_desc| requests
497 // an ICE restart (i.e., new ufrag/pwd). 2. If it requests an ICE restart
498 // and it is an OFFER, remember this in |ice_restart_| so that the next
499 // Local Answer will be created with new ufrag and pwd.
honghaiz503726c2015-07-31 12:37:38 -0700500 bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
501 const SessionDescriptionInterface* new_desc) {
Honghai Zhang04e91462015-12-11 14:26:22 -0800502 if (!old_desc) {
honghaiz503726c2015-07-31 12:37:38 -0700503 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 }
505 const SessionDescription* new_sd = new_desc->description();
506 const SessionDescription* old_sd = old_desc->description();
507 const ContentInfos& contents = new_sd->contents();
508 for (size_t index = 0; index < contents.size(); ++index) {
509 const ContentInfo* cinfo = &contents[index];
510 if (cinfo->rejected) {
511 continue;
512 }
513 // If the content isn't rejected, check if ufrag and password has
514 // changed.
515 const cricket::TransportDescription* new_transport_desc =
516 new_sd->GetTransportDescriptionByName(cinfo->name);
517 const cricket::TransportDescription* old_transport_desc =
518 old_sd->GetTransportDescriptionByName(cinfo->name);
519 if (!new_transport_desc || !old_transport_desc) {
520 // No transport description exist. This is not an ice restart.
521 continue;
522 }
jiayl@webrtc.orgdb397e52014-06-20 16:32:09 +0000523 if (cricket::IceCredentialsChanged(old_transport_desc->ice_ufrag,
524 old_transport_desc->ice_pwd,
525 new_transport_desc->ice_ufrag,
526 new_transport_desc->ice_pwd)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527 LOG(LS_INFO) << "Remote peer request ice restart.";
Honghai Zhang04e91462015-12-11 14:26:22 -0800528 if (new_desc->type() == SessionDescriptionInterface::kOffer) {
529 ice_restart_ = true;
530 }
honghaiz503726c2015-07-31 12:37:38 -0700531 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532 }
533 }
honghaiz503726c2015-07-31 12:37:38 -0700534 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535 }
536
537 private:
538 bool ice_restart_;
539};
540
stefanc1aeaf02015-10-15 07:26:07 -0700541WebRtcSession::WebRtcSession(webrtc::MediaControllerInterface* media_controller,
deadbeefab9b2d12015-10-14 11:33:11 -0700542 rtc::Thread* signaling_thread,
543 rtc::Thread* worker_thread,
544 cricket::PortAllocator* port_allocator)
deadbeefd59daf82015-10-14 15:02:44 -0700545 : signaling_thread_(signaling_thread),
546 worker_thread_(worker_thread),
547 port_allocator_(port_allocator),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548 // RFC 3264: The numeric value of the session id and version in the
549 // o line MUST be representable with a "64 bit signed integer".
550 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700551 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
552 transport_controller_(new cricket::TransportController(signaling_thread,
553 worker_thread,
554 port_allocator)),
stefanc1aeaf02015-10-15 07:26:07 -0700555 media_controller_(media_controller),
556 channel_manager_(media_controller_->channel_manager()),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 ice_observer_(NULL),
558 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700559 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000560 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000561 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000562 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000563 ice_restart_latch_(new IceRestartAnswerLatch),
564 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700565 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
566 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700567 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700568 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700569 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700570 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700571 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700572 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700573 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574}
575
576WebRtcSession::~WebRtcSession() {
tommi0f620f42015-07-09 03:25:02 -0700577 ASSERT(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000578 // Destroy video_channel_ first since it may have a pointer to the
579 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000580 if (video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581 SignalVideoChannelDestroyed();
582 channel_manager_->DestroyVideoChannel(video_channel_.release());
583 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000584 if (voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000585 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200586 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000587 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000588 if (data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589 SignalDataChannelDestroyed();
590 channel_manager_->DestroyDataChannel(data_channel_.release());
591 }
deadbeef057ecf02016-01-20 14:30:43 -0800592 SignalDestroyed();
deadbeefd59daf82015-10-14 15:02:44 -0700593
594 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000595}
596
wu@webrtc.org91053e72013-08-10 07:18:04 +0000597bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000598 const PeerConnectionFactoryInterface::Options& options,
deadbeefcbecd352015-09-23 11:50:27 -0700599 const MediaConstraintsInterface* constraints,
Henrik Boström5e56c592015-08-11 10:33:13 +0200600 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
Henrik Lundin64dad832015-05-11 12:44:23 +0200601 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
602 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700603 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
qiangchen444682a2015-11-24 18:07:56 -0800604 video_options_.disable_prerenderer_smoothing =
605 rtc::Optional<bool>(rtc_configuration.disable_prerenderer_smoothing);
deadbeefd59daf82015-10-14 15:02:44 -0700606 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000607
Henrik Boström87713d02015-08-25 09:53:21 +0200608 // Obtain a certificate from RTCConfiguration if any were provided (optional).
609 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
610 if (!rtc_configuration.certificates.empty()) {
611 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
612 // just picking the first one. The decision should be made based on the DTLS
613 // handshake. The DTLS negotiations need to know about all certificates.
614 certificate = rtc_configuration.certificates[0];
615 }
616
honghaiz1f429e32015-09-28 07:57:34 -0700617 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700618
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000619 // TODO(perkj): Take |constraints| into consideration. Return false if not all
620 // mandatory constraints can be fulfilled. Note that |constraints|
621 // can be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622 bool value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000623
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000624 if (options.disable_encryption) {
625 dtls_enabled_ = false;
626 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200627 // Enable DTLS by default if we have an identity store or a certificate.
628 dtls_enabled_ = (dtls_identity_store || certificate);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000629 // |constraints| can override the default |dtls_enabled_| value.
deadbeefcbecd352015-09-23 11:50:27 -0700630 if (FindConstraint(constraints, MediaConstraintsInterface::kEnableDtlsSrtp,
631 &value, nullptr)) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000632 dtls_enabled_ = value;
633 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000634 }
635
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000637 // It takes precendence over the disable_sctp_data_channels
638 // PeerConnectionFactoryInterface::Options.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 if (FindConstraint(
640 constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
641 &value, NULL) && value) {
642 LOG(LS_INFO) << "Allowing RTP data engine.";
643 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000644 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000645 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000646 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000647 LOG(LS_INFO) << "Allowing SCTP data engine.";
648 data_channel_type_ = cricket::DCT_SCTP;
649 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000650 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651
wu@webrtc.orgde305012013-10-31 15:40:38 +0000652 // Find DSCP constraint.
653 if (FindConstraint(
654 constraints,
655 MediaConstraintsInterface::kEnableDscp,
656 &value, NULL)) {
Karl Wibergbe579832015-11-10 22:34:18 +0100657 audio_options_.dscp = rtc::Optional<bool>(value);
658 video_options_.dscp = rtc::Optional<bool>(value);
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000659 }
660
661 // Find Suspend Below Min Bitrate constraint.
662 if (FindConstraint(
663 constraints,
664 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
665 &value,
666 NULL)) {
Karl Wibergbe579832015-11-10 22:34:18 +0100667 video_options_.suspend_below_min_bitrate = rtc::Optional<bool>(value);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000668 }
669
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000670 SetOptionFromOptionalConstraint(constraints,
671 MediaConstraintsInterface::kScreencastMinBitrate,
nisseb163c3f2016-01-29 01:14:38 -0800672 &video_options_.screencast_min_bitrate_kbps);
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000673
674 // Find constraints for cpu overuse detection.
675 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000676 MediaConstraintsInterface::kCpuOveruseDetection,
677 &video_options_.cpu_overuse_detection);
wu@webrtc.orgcfe5e9c2014-03-27 17:03:58 +0000678
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000679 SetOptionFromOptionalConstraint(constraints,
680 MediaConstraintsInterface::kCombinedAudioVideoBwe,
681 &audio_options_.combined_audio_video_bwe);
682
kwiberg102c6a62015-10-30 02:47:38 -0700683 audio_options_.audio_jitter_buffer_max_packets =
Karl Wibergbe579832015-11-10 22:34:18 +0100684 rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200685
Karl Wibergbe579832015-11-10 22:34:18 +0100686 audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(
687 rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200688
Henrik Boström87713d02015-08-25 09:53:21 +0200689 if (!dtls_enabled_) {
690 // Construct with DTLS disabled.
691 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700692 signaling_thread(), channel_manager_, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200693 } else {
694 // Construct with DTLS enabled.
695 if (!certificate) {
696 // Use the |dtls_identity_store| to generate a certificate.
henrikg91d6ede2015-09-17 00:24:34 -0700697 RTC_DCHECK(dtls_identity_store);
Henrik Boström87713d02015-08-25 09:53:21 +0200698 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
kwiberg0eb15ed2015-12-17 03:04:15 -0800699 signaling_thread(), channel_manager_, std::move(dtls_identity_store),
deadbeefab9b2d12015-10-14 11:33:11 -0700700 this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200701 } else {
702 // Use the already generated certificate.
703 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700704 signaling_thread(), channel_manager_, certificate, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200705 }
706 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000707
Henrik Boströmd8281982015-08-27 10:12:24 +0200708 webrtc_session_desc_factory_->SignalCertificateReady.connect(
709 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000710
wu@webrtc.org97077a32013-10-25 21:18:33 +0000711 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000712 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000713 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000714 port_allocator()->set_candidate_filter(
Henrik Lundin64dad832015-05-11 12:44:23 +0200715 ConvertIceTransportTypeToCandidateFilter(rtc_configuration.type));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700716
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 return true;
718}
719
deadbeefd59daf82015-10-14 15:02:44 -0700720void WebRtcSession::Close() {
721 SetState(STATE_CLOSED);
722 RemoveUnusedChannels(nullptr);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000723 ASSERT(!voice_channel_);
724 ASSERT(!video_channel_);
725 ASSERT(!data_channel_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726}
727
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000728void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
729 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000730}
731
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000732cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
733 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000734}
735
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800736bool WebRtcSession::GetSslRole(const std::string& transport_name,
737 rtc::SSLRole* role) {
deadbeefd59daf82015-10-14 15:02:44 -0700738 if (!local_desc_ || !remote_desc_) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000739 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
740 << "SSL Role of the session.";
741 return false;
742 }
743
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800744 return transport_controller_->GetSslRole(transport_name, role);
745}
746
747bool WebRtcSession::GetSslRole(const cricket::BaseChannel* channel,
748 rtc::SSLRole* role) {
749 return channel && GetSslRole(channel->transport_name(), role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000750}
751
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000752void WebRtcSession::CreateOffer(
753 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700754 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
755 const cricket::MediaSessionOptions& session_options) {
756 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000757}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758
deadbeefab9b2d12015-10-14 11:33:11 -0700759void WebRtcSession::CreateAnswer(
760 CreateSessionDescriptionObserver* observer,
761 const MediaConstraintsInterface* constraints,
762 const cricket::MediaSessionOptions& session_options) {
763 webrtc_session_desc_factory_->CreateAnswer(observer, constraints,
764 session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765}
766
767bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
768 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700769 ASSERT(signaling_thread()->IsCurrent());
770
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000771 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000772 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000773
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000774 // Validate SDP.
775 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
776 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777 }
778
deadbeefd59daf82015-10-14 15:02:44 -0700779 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000780 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700782 initial_offerer_ = true;
783 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 }
785
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000786 cricket::SecurePolicy sdes_policy =
787 webrtc_session_desc_factory_->SdesPolicy();
788 cricket::CryptoType crypto_required = dtls_enabled_ ?
789 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
790 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000792 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000794 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795
796 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000797 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 // TODO(mallinath) - Handle CreateChannel failure, as new local description
799 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000800 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801 }
802
deadbeefcbecd352015-09-23 11:50:27 -0700803 // Remove unused channels if MediaContentDescription is rejected.
804 RemoveUnusedChannels(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000806 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807 return false;
808 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000809
deadbeefd59daf82015-10-14 15:02:44 -0700810 if (remote_desc_) {
811 // Now that we have a local description, we can push down remote candidates.
deadbeefcbecd352015-09-23 11:50:27 -0700812 UseCandidatesInSessionDescription(remote_desc_.get());
813 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814
deadbeefd59daf82015-10-14 15:02:44 -0700815 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000816 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 }
818 return true;
819}
820
821bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
822 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700823 ASSERT(signaling_thread()->IsCurrent());
824
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000825 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000826 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000827
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000828 // Validate SDP.
829 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
830 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000831 }
832
deadbeefd59daf82015-10-14 15:02:44 -0700833 rtc::scoped_ptr<SessionDescriptionInterface> old_remote_desc(
834 remote_desc_.release());
835 remote_desc_.reset(desc_temp.release());
836
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000838 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 if (action == kOffer && !CreateChannels(desc->description())) {
840 // TODO(mallinath) - Handle CreateChannel failure, as new local description
841 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000842 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843 }
844
deadbeefcbecd352015-09-23 11:50:27 -0700845 // Remove unused channels if MediaContentDescription is rejected.
846 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847
848 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
849 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000850 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851 return false;
852 }
853
deadbeefd59daf82015-10-14 15:02:44 -0700854 if (local_desc_ && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000855 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000856 }
857
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 // Check if this new SessionDescription contains new ice ufrag and password
859 // that indicates the remote peer requests ice restart.
honghaiz503726c2015-07-31 12:37:38 -0700860 bool ice_restart =
deadbeefd59daf82015-10-14 15:02:44 -0700861 ice_restart_latch_->CheckForRemoteIceRestart(old_remote_desc.get(), desc);
honghaiz503726c2015-07-31 12:37:38 -0700862 // We retain all received candidates only if ICE is not restarted.
863 // When ICE is restarted, all previous candidates belong to an old generation
864 // and should not be kept.
deadbeefd59daf82015-10-14 15:02:44 -0700865 // TODO(deadbeef): This goes against the W3C spec which says the remote
866 // description should only contain candidates from the last set remote
867 // description plus any candidates added since then. We should remove this
868 // once we're sure it won't break anything.
honghaiz503726c2015-07-31 12:37:38 -0700869 if (!ice_restart) {
870 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
deadbeefd59daf82015-10-14 15:02:44 -0700871 old_remote_desc.get(), desc);
honghaiz503726c2015-07-31 12:37:38 -0700872 }
873
deadbeefd59daf82015-10-14 15:02:44 -0700874 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000875 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000876 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000877
878 // Set the the ICE connection state to connecting since the connection may
879 // become writable with peer reflexive candidates before any remote candidate
880 // is signaled.
881 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
882 // is to have a new signal the indicates a change in checking state from the
883 // transport and expose a new checking() member from transport that can be
884 // read to determine the current checking state. The existing SignalConnecting
885 // actually means "gathering candidates", so cannot be be used here.
886 if (desc->type() != SessionDescriptionInterface::kOffer &&
887 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
888 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
889 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000890 return true;
891}
892
deadbeefd59daf82015-10-14 15:02:44 -0700893void WebRtcSession::LogState(State old_state, State new_state) {
894 LOG(LS_INFO) << "Session:" << id()
895 << " Old state:" << GetStateString(old_state)
896 << " New state:" << GetStateString(new_state);
897}
898
899void WebRtcSession::SetState(State state) {
900 ASSERT(signaling_thread_->IsCurrent());
901 if (state != state_) {
902 LogState(state_, state);
903 state_ = state;
904 SignalState(this, state_);
905 }
906}
907
908void WebRtcSession::SetError(Error error, const std::string& error_desc) {
909 ASSERT(signaling_thread_->IsCurrent());
910 if (error != error_) {
911 error_ = error;
912 error_desc_ = error_desc;
913 }
914}
915
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916bool WebRtcSession::UpdateSessionState(
917 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000918 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700919 ASSERT(signaling_thread()->IsCurrent());
920
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921 // If there's already a pending error then no state transition should happen.
922 // But all call-sites should be verifying this before calling us!
deadbeefd59daf82015-10-14 15:02:44 -0700923 ASSERT(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000924 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000926 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
927 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 }
deadbeefd59daf82015-10-14 15:02:44 -0700929 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
930 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000931 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700932 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000933 }
deadbeefd59daf82015-10-14 15:02:44 -0700934 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000935 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 }
937 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000938 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
939 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000940 }
941 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700942 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
943 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000944 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700945 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000946 }
deadbeefd59daf82015-10-14 15:02:44 -0700947 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000948 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949 }
950 } else if (action == kAnswer) {
deadbeefcbecd352015-09-23 11:50:27 -0700951 const cricket::ContentGroup* local_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700952 local_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700953 const cricket::ContentGroup* remote_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700954 remote_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700955 if (local_bundle && remote_bundle) {
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800956 // The answerer decides the transport to bundle on.
deadbeefcbecd352015-09-23 11:50:27 -0700957 const cricket::ContentGroup* answer_bundle =
958 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
959 if (!EnableBundle(*answer_bundle)) {
960 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
961 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
962 }
963 }
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800964 // Only push down the transport description after enabling BUNDLE; we don't
965 // want to push down a description on a transport about to be destroyed.
966 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
967 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
968 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700970 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000971 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700972 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000973 }
deadbeefd59daf82015-10-14 15:02:44 -0700974 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000975 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976 }
977 }
978 return true;
979}
980
981WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
982 if (type == SessionDescriptionInterface::kOffer) {
983 return WebRtcSession::kOffer;
984 } else if (type == SessionDescriptionInterface::kPrAnswer) {
985 return WebRtcSession::kPrAnswer;
986 } else if (type == SessionDescriptionInterface::kAnswer) {
987 return WebRtcSession::kAnswer;
988 }
989 ASSERT(false && "unknown action type");
990 return WebRtcSession::kOffer;
991}
992
deadbeefd59daf82015-10-14 15:02:44 -0700993bool WebRtcSession::PushdownMediaDescription(
994 cricket::ContentAction action,
995 cricket::ContentSource source,
996 std::string* err) {
997 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
998 if (!ch) {
999 return true;
1000 } else if (source == cricket::CS_LOCAL) {
1001 return ch->PushdownLocalDescription(local_desc_->description(), action,
1002 err);
1003 } else {
1004 return ch->PushdownRemoteDescription(remote_desc_->description(), action,
1005 err);
1006 }
1007 };
1008
1009 return (set_content(voice_channel()) &&
1010 set_content(video_channel()) &&
1011 set_content(data_channel()));
1012}
1013
1014bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
1015 cricket::ContentAction action,
1016 std::string* error_desc) {
1017 RTC_DCHECK(signaling_thread()->IsCurrent());
1018
1019 if (source == cricket::CS_LOCAL) {
1020 return PushdownLocalTransportDescription(local_desc_->description(), action,
1021 error_desc);
1022 }
1023 return PushdownRemoteTransportDescription(remote_desc_->description(), action,
1024 error_desc);
1025}
1026
1027bool WebRtcSession::PushdownLocalTransportDescription(
1028 const SessionDescription* sdesc,
1029 cricket::ContentAction action,
1030 std::string* err) {
1031 RTC_DCHECK(signaling_thread()->IsCurrent());
1032
1033 if (!sdesc) {
1034 return false;
1035 }
1036
1037 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1038 if (!transport_controller_->SetLocalTransportDescription(
1039 tinfo.content_name, tinfo.description, action, err)) {
1040 return false;
1041 }
1042 }
1043
1044 return true;
1045}
1046
1047bool WebRtcSession::PushdownRemoteTransportDescription(
1048 const SessionDescription* sdesc,
1049 cricket::ContentAction action,
1050 std::string* err) {
1051 RTC_DCHECK(signaling_thread()->IsCurrent());
1052
1053 if (!sdesc) {
1054 return false;
1055 }
1056
1057 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1058 if (!transport_controller_->SetRemoteTransportDescription(
1059 tinfo.content_name, tinfo.description, action, err)) {
1060 return false;
1061 }
1062 }
1063
1064 return true;
1065}
1066
1067bool WebRtcSession::GetTransportDescription(
1068 const SessionDescription* description,
1069 const std::string& content_name,
1070 cricket::TransportDescription* tdesc) {
1071 if (!description || !tdesc) {
1072 return false;
1073 }
1074 const TransportInfo* transport_info =
1075 description->GetTransportInfoByName(content_name);
1076 if (!transport_info) {
1077 return false;
1078 }
1079 *tdesc = transport_info->description;
1080 return true;
1081}
1082
1083bool WebRtcSession::GetTransportStats(SessionStats* stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001084 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001085 return (GetChannelTransportStats(voice_channel(), stats) &&
1086 GetChannelTransportStats(video_channel(), stats) &&
1087 GetChannelTransportStats(data_channel(), stats));
1088}
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001089
deadbeefcbecd352015-09-23 11:50:27 -07001090bool WebRtcSession::GetChannelTransportStats(cricket::BaseChannel* ch,
deadbeefd59daf82015-10-14 15:02:44 -07001091 SessionStats* stats) {
deadbeefcbecd352015-09-23 11:50:27 -07001092 ASSERT(signaling_thread()->IsCurrent());
1093 if (!ch) {
1094 // Not using this channel.
1095 return true;
1096 }
1097
1098 const std::string& content_name = ch->content_name();
1099 const std::string& transport_name = ch->transport_name();
1100 stats->proxy_to_transport[content_name] = transport_name;
1101 if (stats->transport_stats.find(transport_name) !=
1102 stats->transport_stats.end()) {
1103 // Transport stats already done for this transport.
1104 return true;
1105 }
1106
1107 cricket::TransportStats tstats;
deadbeefd59daf82015-10-14 15:02:44 -07001108 if (!transport_controller_->GetStats(transport_name, &tstats)) {
deadbeefcbecd352015-09-23 11:50:27 -07001109 return false;
1110 }
1111
1112 stats->transport_stats[transport_name] = tstats;
1113 return true;
1114}
1115
1116bool WebRtcSession::GetLocalCertificate(
1117 const std::string& transport_name,
1118 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1119 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001120 return transport_controller_->GetLocalCertificate(transport_name,
1121 certificate);
deadbeefcbecd352015-09-23 11:50:27 -07001122}
1123
1124bool WebRtcSession::GetRemoteSSLCertificate(const std::string& transport_name,
1125 rtc::SSLCertificate** cert) {
1126 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001127 return transport_controller_->GetRemoteSSLCertificate(transport_name, cert);
deadbeefcbecd352015-09-23 11:50:27 -07001128}
1129
1130cricket::BaseChannel* WebRtcSession::GetChannel(
1131 const std::string& content_name) {
1132 if (voice_channel() && voice_channel()->content_name() == content_name) {
1133 return voice_channel();
1134 }
1135 if (video_channel() && video_channel()->content_name() == content_name) {
1136 return video_channel();
1137 }
1138 if (data_channel() && data_channel()->content_name() == content_name) {
1139 return data_channel();
1140 }
1141 return nullptr;
1142}
1143
1144bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1145 const std::string* first_content_name = bundle.FirstContentName();
1146 if (!first_content_name) {
1147 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1148 return false;
1149 }
1150 const std::string& transport_name = *first_content_name;
1151 cricket::BaseChannel* first_channel = GetChannel(transport_name);
1152
1153 auto maybe_set_transport = [this, bundle, transport_name,
1154 first_channel](cricket::BaseChannel* ch) {
1155 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001156 return true;
1157 }
1158
deadbeefcbecd352015-09-23 11:50:27 -07001159 if (ch->transport_name() == transport_name) {
1160 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1161 << " on " << transport_name << ".";
1162 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001163 }
torbjornga81a42f2015-09-23 02:16:58 -07001164
deadbeefcbecd352015-09-23 11:50:27 -07001165 if (!ch->SetTransport(transport_name)) {
1166 LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name();
1167 return false;
1168 }
1169 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1170 << transport_name << ".";
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001171 return true;
1172 };
1173
deadbeefcbecd352015-09-23 11:50:27 -07001174 if (!maybe_set_transport(voice_channel()) ||
1175 !maybe_set_transport(video_channel()) ||
1176 !maybe_set_transport(data_channel())) {
1177 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001178 }
deadbeefcbecd352015-09-23 11:50:27 -07001179
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001180 return true;
1181}
1182
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001183bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001184 if (!remote_desc_) {
1185 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1186 << "without any remote session description.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001187 return false;
1188 }
1189
1190 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001191 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001192 return false;
1193 }
1194
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001195 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001196 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1197 if (!valid) {
1198 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001199 }
1200
1201 // Add this candidate to the remote session description.
1202 if (!remote_desc_->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001203 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001204 return false;
1205 }
1206
deadbeefd59daf82015-10-14 15:02:44 -07001207 if (ready) {
1208 return UseCandidate(candidate);
1209 } else {
1210 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1211 return true;
1212 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001213}
1214
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001215bool WebRtcSession::SetIceTransports(
1216 PeerConnectionInterface::IceTransportsType type) {
1217 return port_allocator()->set_candidate_filter(
1218 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001219}
1220
deadbeefd59daf82015-10-14 15:02:44 -07001221cricket::IceConfig WebRtcSession::ParseIceConfig(
1222 const PeerConnectionInterface::RTCConfiguration& config) const {
1223 cricket::IceConfig ice_config;
1224 ice_config.receiving_timeout_ms = config.ice_connection_receiving_timeout;
Honghai Zhang381b4212015-12-04 12:24:03 -08001225 ice_config.backup_connection_ping_interval =
1226 config.ice_backup_candidate_pair_ping_interval;
deadbeefd59daf82015-10-14 15:02:44 -07001227 ice_config.gather_continually = (config.continual_gathering_policy ==
1228 PeerConnectionInterface::GATHER_CONTINUALLY);
1229 return ice_config;
1230}
1231
1232void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1233 transport_controller_->SetIceConfig(config);
1234}
1235
1236void WebRtcSession::MaybeStartGathering() {
1237 transport_controller_->MaybeStartGathering();
1238}
1239
Peter Boström0c4e06b2015-10-07 12:23:21 +02001240bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1241 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001242 if (!local_desc_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001243 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001244 }
1245 return webrtc::GetTrackIdBySsrc(local_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001246}
1247
Peter Boström0c4e06b2015-10-07 12:23:21 +02001248bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1249 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001250 if (!remote_desc_) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001251 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001252 }
1253 return webrtc::GetTrackIdBySsrc(remote_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254}
1255
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001256std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001257 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001258 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001259 return desc.str();
1260}
1261
solenbergd4cec0d2015-10-09 08:55:48 -07001262void WebRtcSession::SetAudioPlayout(uint32_t ssrc, bool enable) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001263 ASSERT(signaling_thread()->IsCurrent());
1264 if (!voice_channel_) {
1265 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
1266 return;
1267 }
solenberg4bac9c52015-10-09 02:32:53 -07001268 if (!voice_channel_->SetOutputVolume(ssrc, enable ? 1 : 0)) {
1269 // Allow that SetOutputVolume fail if |enable| is false but assert
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001270 // otherwise. This in the normal case when the underlying media channel has
1271 // already been deleted.
1272 ASSERT(enable == false);
1273 }
1274}
1275
Peter Boström0c4e06b2015-10-07 12:23:21 +02001276void WebRtcSession::SetAudioSend(uint32_t ssrc,
1277 bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001278 const cricket::AudioOptions& options,
1279 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280 ASSERT(signaling_thread()->IsCurrent());
1281 if (!voice_channel_) {
1282 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
1283 return;
1284 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001285 if (!voice_channel_->SetAudioSend(ssrc, enable, &options, renderer)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001286 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001287 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001288}
1289
Peter Boström0c4e06b2015-10-07 12:23:21 +02001290void WebRtcSession::SetAudioPlayoutVolume(uint32_t ssrc, double volume) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001291 ASSERT(signaling_thread()->IsCurrent());
1292 ASSERT(volume >= 0 && volume <= 10);
1293 if (!voice_channel_) {
1294 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1295 return;
1296 }
1297
solenberg4bac9c52015-10-09 02:32:53 -07001298 if (!voice_channel_->SetOutputVolume(ssrc, volume)) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001299 ASSERT(false);
solenbergbb741b32015-09-07 03:56:38 -07001300 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001301}
1302
deadbeef2d110be2016-01-13 12:00:26 -08001303void WebRtcSession::SetRawAudioSink(uint32_t ssrc,
1304 rtc::scoped_ptr<AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001305 ASSERT(signaling_thread()->IsCurrent());
1306 if (!voice_channel_)
1307 return;
1308
deadbeef2d110be2016-01-13 12:00:26 -08001309 voice_channel_->SetRawAudioSink(ssrc, std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001310}
1311
Peter Boström0c4e06b2015-10-07 12:23:21 +02001312bool WebRtcSession::SetCaptureDevice(uint32_t ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001313 cricket::VideoCapturer* camera) {
1314 ASSERT(signaling_thread()->IsCurrent());
1315
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001316 if (!video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001317 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1318 // support video.
1319 LOG(LS_WARNING) << "Video not used in this call.";
1320 return false;
1321 }
1322 if (!video_channel_->SetCapturer(ssrc, camera)) {
1323 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1324 // This in the normal case when the underlying media channel has already
1325 // been deleted.
1326 ASSERT(camera == NULL);
1327 return false;
1328 }
1329 return true;
1330}
1331
nisse08582ff2016-02-04 01:24:52 -08001332void WebRtcSession::SetVideoPlayout(
1333 uint32_t ssrc,
1334 bool enable,
1335 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001336 ASSERT(signaling_thread()->IsCurrent());
1337 if (!video_channel_) {
1338 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1339 return;
1340 }
nisse08582ff2016-02-04 01:24:52 -08001341 if (!video_channel_->SetSink(ssrc, enable ? sink : NULL)) {
1342 // Allow that SetSink fail if |sink| is NULL but assert otherwise.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001343 // This in the normal case when the underlying media channel has already
1344 // been deleted.
nisse08582ff2016-02-04 01:24:52 -08001345 ASSERT(sink == NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346 }
1347}
1348
Peter Boström0c4e06b2015-10-07 12:23:21 +02001349void WebRtcSession::SetVideoSend(uint32_t ssrc,
1350 bool enable,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001351 const cricket::VideoOptions* options) {
1352 ASSERT(signaling_thread()->IsCurrent());
1353 if (!video_channel_) {
1354 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1355 return;
1356 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001357 if (!video_channel_->SetVideoSend(ssrc, enable, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001358 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1359 // This in the normal case when the underlying media channel has already
1360 // been deleted.
1361 ASSERT(enable == false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001362 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001363}
1364
1365bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1366 ASSERT(signaling_thread()->IsCurrent());
1367 if (!voice_channel_) {
1368 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1369 return false;
1370 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001371 uint32_t send_ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001372 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1373 // exists.
deadbeefd59daf82015-10-14 15:02:44 -07001374 if (!local_desc_ ||
1375 !GetAudioSsrcByTrackId(local_desc_->description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001376 &send_ssrc)) {
1377 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1378 return false;
1379 }
1380 return voice_channel_->CanInsertDtmf();
1381}
1382
1383bool WebRtcSession::InsertDtmf(const std::string& track_id,
1384 int code, int duration) {
1385 ASSERT(signaling_thread()->IsCurrent());
1386 if (!voice_channel_) {
1387 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1388 return false;
1389 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001390 uint32_t send_ssrc = 0;
deadbeefd59daf82015-10-14 15:02:44 -07001391 if (!VERIFY(local_desc_ && GetAudioSsrcByTrackId(local_desc_->description(),
1392 track_id, &send_ssrc))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001393 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1394 return false;
1395 }
solenberg1d63dd02015-12-02 12:35:09 -08001396 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001397 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1398 return false;
1399 }
1400 return true;
1401}
1402
1403sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
deadbeef057ecf02016-01-20 14:30:43 -08001404 return &SignalDestroyed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001405}
1406
wu@webrtc.org78187522013-10-07 23:32:02 +00001407bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001408 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001409 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001410 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001411 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1412 return false;
1413 }
1414 return data_channel_->SendData(params, payload, result);
1415}
1416
1417bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001418 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001419 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1420 return false;
1421 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001422 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1423 &DataChannel::OnChannelReady);
1424 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1425 &DataChannel::OnDataReceived);
deadbeefab9b2d12015-10-14 11:33:11 -07001426 data_channel_->SignalStreamClosedRemotely.connect(
1427 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
wu@webrtc.org78187522013-10-07 23:32:02 +00001428 return true;
1429}
1430
1431void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001432 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001433 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1434 return;
1435 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001436 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1437 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
deadbeefab9b2d12015-10-14 11:33:11 -07001438 data_channel_->SignalStreamClosedRemotely.disconnect(webrtc_data_channel);
wu@webrtc.org78187522013-10-07 23:32:02 +00001439}
1440
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001441void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001442 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001443 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1444 return;
1445 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001446 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1447 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001448}
1449
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001450void WebRtcSession::RemoveSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001451 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001452 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1453 << "NULL.";
1454 return;
1455 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001456 data_channel_->RemoveRecvStream(sid);
1457 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001458}
1459
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001460bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001461 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001462}
1463
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001464cricket::DataChannelType WebRtcSession::data_channel_type() const {
1465 return data_channel_type_;
1466}
1467
wu@webrtc.org91053e72013-08-10 07:18:04 +00001468bool WebRtcSession::IceRestartPending() const {
1469 return ice_restart_latch_->Get();
1470}
1471
1472void WebRtcSession::ResetIceRestartLatch() {
1473 ice_restart_latch_->Reset();
1474}
1475
Henrik Boströmd8281982015-08-27 10:12:24 +02001476void WebRtcSession::OnCertificateReady(
1477 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001478 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001479}
1480
Henrik Boströmd8281982015-08-27 10:12:24 +02001481bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001482 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001483}
1484
deadbeefcbecd352015-09-23 11:50:27 -07001485const rtc::scoped_refptr<rtc::RTCCertificate>&
1486WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001487 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001488}
1489
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001490void WebRtcSession::SetIceConnectionState(
1491 PeerConnectionInterface::IceConnectionState state) {
1492 if (ice_connection_state_ == state) {
1493 return;
1494 }
1495
1496 // ASSERT that the requested transition is allowed. Note that
1497 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1498 // within PeerConnection). This switch statement should compile away when
1499 // ASSERTs are disabled.
deadbeefcbecd352015-09-23 11:50:27 -07001500 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1501 << " => " << state;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001502 switch (ice_connection_state_) {
1503 case PeerConnectionInterface::kIceConnectionNew:
1504 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1505 break;
1506 case PeerConnectionInterface::kIceConnectionChecking:
1507 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1508 state == PeerConnectionInterface::kIceConnectionConnected);
1509 break;
1510 case PeerConnectionInterface::kIceConnectionConnected:
1511 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1512 state == PeerConnectionInterface::kIceConnectionChecking ||
1513 state == PeerConnectionInterface::kIceConnectionCompleted);
1514 break;
1515 case PeerConnectionInterface::kIceConnectionCompleted:
1516 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1517 state == PeerConnectionInterface::kIceConnectionDisconnected);
1518 break;
1519 case PeerConnectionInterface::kIceConnectionFailed:
1520 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1521 break;
1522 case PeerConnectionInterface::kIceConnectionDisconnected:
1523 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1524 state == PeerConnectionInterface::kIceConnectionConnected ||
1525 state == PeerConnectionInterface::kIceConnectionCompleted ||
1526 state == PeerConnectionInterface::kIceConnectionFailed);
1527 break;
1528 case PeerConnectionInterface::kIceConnectionClosed:
1529 ASSERT(false);
1530 break;
1531 default:
1532 ASSERT(false);
1533 break;
1534 }
1535
1536 ice_connection_state_ = state;
1537 if (ice_observer_) {
1538 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1539 }
1540}
1541
deadbeefcbecd352015-09-23 11:50:27 -07001542void WebRtcSession::OnTransportControllerConnectionState(
1543 cricket::IceConnectionState state) {
1544 switch (state) {
1545 case cricket::kIceConnectionConnecting:
1546 // If the current state is Connected or Completed, then there were
1547 // writable channels but now there are not, so the next state must
1548 // be Disconnected.
1549 // kIceConnectionConnecting is currently used as the default,
1550 // un-connected state by the TransportController, so its only use is
1551 // detecting disconnections.
1552 if (ice_connection_state_ ==
1553 PeerConnectionInterface::kIceConnectionConnected ||
1554 ice_connection_state_ ==
1555 PeerConnectionInterface::kIceConnectionCompleted) {
1556 SetIceConnectionState(
1557 PeerConnectionInterface::kIceConnectionDisconnected);
1558 }
torbjornga81a42f2015-09-23 02:16:58 -07001559 break;
deadbeefcbecd352015-09-23 11:50:27 -07001560 case cricket::kIceConnectionFailed:
1561 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1562 break;
1563 case cricket::kIceConnectionConnected:
1564 LOG(LS_INFO) << "Changing to ICE connected state because "
1565 << "all transports are writable.";
1566 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1567 break;
1568 case cricket::kIceConnectionCompleted:
1569 LOG(LS_INFO) << "Changing to ICE completed state because "
1570 << "all transports are complete.";
1571 if (ice_connection_state_ !=
1572 PeerConnectionInterface::kIceConnectionConnected) {
1573 // If jumping directly from "checking" to "connected",
1574 // signal "connected" first.
1575 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1576 }
1577 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1578 if (metrics_observer_) {
1579 ReportTransportStats();
1580 }
1581 break;
1582 default:
1583 ASSERT(false);
torbjornga81a42f2015-09-23 02:16:58 -07001584 }
deadbeefcbecd352015-09-23 11:50:27 -07001585}
1586
1587void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001588 SetIceConnectionReceiving(receiving);
1589}
1590
1591void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1592 if (ice_connection_receiving_ == receiving) {
1593 return;
1594 }
1595 ice_connection_receiving_ = receiving;
1596 if (ice_observer_) {
1597 ice_observer_->OnIceConnectionReceivingChange(receiving);
1598 }
1599}
1600
deadbeefcbecd352015-09-23 11:50:27 -07001601void WebRtcSession::OnTransportControllerCandidatesGathered(
1602 const std::string& transport_name,
1603 const cricket::Candidates& candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001604 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001605 int sdp_mline_index;
1606 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1607 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1608 << transport_name << " not found";
1609 return;
1610 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001611
deadbeefcbecd352015-09-23 11:50:27 -07001612 for (cricket::Candidates::const_iterator citer = candidates.begin();
1613 citer != candidates.end(); ++citer) {
1614 // Use transport_name as the candidate media id.
1615 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1616 if (ice_observer_) {
1617 ice_observer_->OnIceCandidate(&candidate);
1618 }
1619 if (local_desc_) {
1620 local_desc_->AddCandidate(&candidate);
1621 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001622 }
1623}
1624
1625// Enabling voice and video channel.
1626void WebRtcSession::EnableChannels() {
1627 if (voice_channel_ && !voice_channel_->enabled())
1628 voice_channel_->Enable(true);
1629
1630 if (video_channel_ && !video_channel_->enabled())
1631 video_channel_->Enable(true);
1632
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001633 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001634 data_channel_->Enable(true);
1635}
1636
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001637// Returns the media index for a local ice candidate given the content name.
1638bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1639 int* sdp_mline_index) {
deadbeefd59daf82015-10-14 15:02:44 -07001640 if (!local_desc_ || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001641 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001642 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001643
1644 bool content_found = false;
deadbeefd59daf82015-10-14 15:02:44 -07001645 const ContentInfos& contents = local_desc_->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001646 for (size_t index = 0; index < contents.size(); ++index) {
1647 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001648 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649 content_found = true;
1650 break;
1651 }
1652 }
1653 return content_found;
1654}
1655
1656bool WebRtcSession::UseCandidatesInSessionDescription(
1657 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001658 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001659 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001660 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001661 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001662
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001663 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1664 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001665 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001666 const IceCandidateInterface* candidate = candidates->at(n);
1667 bool valid = false;
1668 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1669 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001670 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1671 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001672 }
1673 continue;
1674 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001675 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001676 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001677 break;
deadbeefd59daf82015-10-14 15:02:44 -07001678 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001679 }
1680 }
1681 return ret;
1682}
1683
1684bool WebRtcSession::UseCandidate(
1685 const IceCandidateInterface* candidate) {
1686
1687 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeefd59daf82015-10-14 15:02:44 -07001688 size_t remote_content_size = remote_desc_->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001689 if (mediacontent_index >= remote_content_size) {
1690 LOG(LS_ERROR)
1691 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1692 return false;
1693 }
1694
1695 cricket::ContentInfo content =
deadbeefd59daf82015-10-14 15:02:44 -07001696 remote_desc_->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001697 std::vector<cricket::Candidate> candidates;
1698 candidates.push_back(candidate->candidate());
1699 // Invoking BaseSession method to handle remote candidates.
1700 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001701 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1702 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001703 // Candidates successfully submitted for checking.
1704 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1705 ice_connection_state_ ==
1706 PeerConnectionInterface::kIceConnectionDisconnected) {
1707 // If state is New, then the session has just gotten its first remote ICE
1708 // candidates, so go to Checking.
1709 // If state is Disconnected, the session is re-using old candidates or
1710 // receiving additional ones, so go to Checking.
1711 // If state is Connected, stay Connected.
1712 // TODO(bemasc): If state is Connected, and the new candidates are for a
1713 // newly added transport, then the state actually _should_ move to
1714 // checking. Add a way to distinguish that case.
1715 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1716 }
1717 // TODO(bemasc): If state is Completed, go back to Connected.
1718 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001719 if (!error.empty()) {
1720 LOG(LS_WARNING) << error;
1721 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722 }
1723 return true;
1724}
1725
deadbeefcbecd352015-09-23 11:50:27 -07001726void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001727 // Destroy video_channel_ first since it may have a pointer to the
1728 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001729 const cricket::ContentInfo* video_info =
1730 cricket::GetFirstVideoContent(desc);
1731 if ((!video_info || video_info->rejected) && video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732 SignalVideoChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001733 channel_manager_->DestroyVideoChannel(video_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001734 }
1735
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001736 const cricket::ContentInfo* voice_info =
1737 cricket::GetFirstAudioContent(desc);
1738 if ((!voice_info || voice_info->rejected) && voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001739 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001740 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001741 }
1742
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001743 const cricket::ContentInfo* data_info =
1744 cricket::GetFirstDataContent(desc);
1745 if ((!data_info || data_info->rejected) && data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001746 SignalDataChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001747 channel_manager_->DestroyDataChannel(data_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001748 }
1749}
1750
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001751// TODO(mallinath) - Add a correct error code if the channels are not created
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001752// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001753bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001754 // Creating the media channels and transport proxies.
1755 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1756 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001757 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001758 LOG(LS_ERROR) << "Failed to create voice channel.";
1759 return false;
1760 }
1761 }
1762
1763 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1764 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001765 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766 LOG(LS_ERROR) << "Failed to create video channel.";
1767 return false;
1768 }
1769 }
1770
1771 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1772 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001773 data && !data->rejected && !data_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001774 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775 LOG(LS_ERROR) << "Failed to create data channel.";
1776 return false;
1777 }
1778 }
1779
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001780 if (rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire) {
1781 if (voice_channel()) {
1782 voice_channel()->ActivateRtcpMux();
1783 }
1784 if (video_channel()) {
1785 video_channel()->ActivateRtcpMux();
1786 }
1787 if (data_channel()) {
1788 data_channel()->ActivateRtcpMux();
1789 }
1790 }
1791
deadbeefcbecd352015-09-23 11:50:27 -07001792 // Enable BUNDLE immediately when kBundlePolicyMaxBundle is in effect.
Donald Curtis0e209b02015-03-24 09:29:54 -07001793 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
Peter Thatcher4eddf182015-04-30 10:55:59 -07001794 const cricket::ContentGroup* bundle_group = desc->GetGroupByName(
1795 cricket::GROUP_TYPE_BUNDLE);
1796 if (!bundle_group) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001797 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1798 return false;
1799 }
deadbeefcbecd352015-09-23 11:50:27 -07001800 if (!EnableBundle(*bundle_group)) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001801 LOG(LS_WARNING) << "max-bundle failed to enable bundling.";
1802 return false;
1803 }
1804 }
1805
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001806 return true;
1807}
1808
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001809bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001810 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001811 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001812 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001813 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001814 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001815 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001816
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001817 voice_channel_->SignalDtlsSetupFailure.connect(
1818 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001819
1820 SignalVoiceChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001821 voice_channel_->transport_channel()->SignalSentPacket.connect(
1822 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001823 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001824}
1825
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001826bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001827 video_channel_.reset(channel_manager_->CreateVideoChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001828 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001829 video_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001830 if (!video_channel_) {
1831 return false;
1832 }
1833
1834 video_channel_->SignalDtlsSetupFailure.connect(
1835 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001836
1837 SignalVideoChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001838 video_channel_->transport_channel()->SignalSentPacket.connect(
1839 this, &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001840 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001841}
1842
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001843bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001844 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845 data_channel_.reset(channel_manager_->CreateDataChannel(
deadbeefd59daf82015-10-14 15:02:44 -07001846 transport_controller_.get(), content->name, !sctp, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001847 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001848 return false;
1849 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001850
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001851 if (sctp) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001852 data_channel_->SignalDataReceived.connect(
1853 this, &WebRtcSession::OnDataChannelMessageReceived);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001854 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001855
1856 data_channel_->SignalDtlsSetupFailure.connect(
1857 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001858
1859 SignalDataChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001860 data_channel_->transport_channel()->SignalSentPacket.connect(
1861 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001862 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001863}
1864
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001865void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
deadbeefd59daf82015-10-14 15:02:44 -07001866 SetError(ERROR_TRANSPORT,
1867 rtcp ? kDtlsSetupFailureRtcp : kDtlsSetupFailureRtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001868}
1869
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001870void WebRtcSession::OnDataChannelMessageReceived(
1871 cricket::DataChannel* channel,
1872 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001873 const rtc::Buffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001874 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1875 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
1876 // Received OPEN message; parse and signal that a new data channel should
1877 // be created.
1878 std::string label;
1879 InternalDataChannelInit config;
1880 config.id = params.ssrc;
1881 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
1882 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
1883 << params.ssrc;
1884 return;
1885 }
1886 config.open_handshake_role = InternalDataChannelInit::kAcker;
1887 SignalDataChannelOpenMessage(label, config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001888 }
deadbeefab9b2d12015-10-14 11:33:11 -07001889 // Otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001890}
1891
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001892// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001893bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001894 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1895 if (!bundle_enabled)
1896 return true;
1897
1898 const cricket::ContentGroup* bundle_group =
1899 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1900 ASSERT(bundle_group != NULL);
1901
1902 const cricket::ContentInfos& contents = desc->contents();
1903 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1904 citer != contents.end(); ++citer) {
1905 const cricket::ContentInfo* content = (&*citer);
1906 ASSERT(content != NULL);
1907 if (bundle_group->HasContentName(content->name) &&
1908 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1909 if (!HasRtcpMuxEnabled(content))
1910 return false;
1911 }
1912 }
1913 // RTCP-MUX is enabled in all the contents.
1914 return true;
1915}
1916
1917bool WebRtcSession::HasRtcpMuxEnabled(
1918 const cricket::ContentInfo* content) {
1919 const cricket::MediaContentDescription* description =
1920 static_cast<cricket::MediaContentDescription*>(content->description);
1921 return description->rtcp_mux();
1922}
1923
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001924bool WebRtcSession::ValidateSessionDescription(
1925 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001926 cricket::ContentSource source, std::string* err_desc) {
1927 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07001928 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001929 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001930 }
1931
1932 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001933 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001934 }
1935
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001936 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001937 Action action = GetAction(sdesc->type());
1938 if (source == cricket::CS_LOCAL) {
1939 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001940 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001941 } else {
1942 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001943 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001944 }
1945
1946 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001947 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001948 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1949 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001950 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001951 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001952 }
1953
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001954 // Verify ice-ufrag and ice-pwd.
1955 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001956 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001957 }
1958
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001959 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001960 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001961 }
1962
1963 // Verify m-lines in Answer when compared against Offer.
1964 if (action == kAnswer) {
1965 const cricket::SessionDescription* offer_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001966 (source == cricket::CS_LOCAL) ? remote_desc_->description()
1967 : local_desc_->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001968 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001969 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001970 }
1971 }
1972
1973 return true;
1974}
1975
1976bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1977 return ((action == kOffer && state() == STATE_INIT) ||
1978 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07001979 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001980 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001981 (action == kOffer && state() == STATE_INPROGRESS) ||
1982 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001983 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
1984 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
1985 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
1986 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001987}
1988
1989bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1990 return ((action == kOffer && state() == STATE_INIT) ||
1991 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001992 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001993 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001994 (action == kOffer && state() == STATE_INPROGRESS) ||
1995 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07001996 (action == kAnswer && state() == STATE_SENTOFFER) ||
1997 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
1998 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
1999 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002000}
2001
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002002std::string WebRtcSession::GetSessionErrorMsg() {
2003 std::ostringstream desc;
2004 desc << kSessionError << GetErrorCodeString(error()) << ". ";
2005 desc << kSessionErrorDesc << error_desc() << ".";
2006 return desc.str();
2007}
2008
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002009// We need to check the local/remote description for the Transport instead of
2010// the session, because a new Transport added during renegotiation may have
2011// them unset while the session has them set from the previous negotiation.
2012// Not doing so may trigger the auto generation of transport description and
2013// mess up DTLS identity information, ICE credential, etc.
2014bool WebRtcSession::ReadyToUseRemoteCandidate(
2015 const IceCandidateInterface* candidate,
2016 const SessionDescriptionInterface* remote_desc,
2017 bool* valid) {
2018 *valid = true;;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002019
2020 const SessionDescriptionInterface* current_remote_desc =
deadbeefd59daf82015-10-14 15:02:44 -07002021 remote_desc ? remote_desc : remote_desc_.get();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002022
deadbeefd59daf82015-10-14 15:02:44 -07002023 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002024 return false;
deadbeefd59daf82015-10-14 15:02:44 -07002025 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002026
2027 size_t mediacontent_index =
2028 static_cast<size_t>(candidate->sdp_mline_index());
2029 size_t remote_content_size =
2030 current_remote_desc->description()->contents().size();
2031 if (mediacontent_index >= remote_content_size) {
2032 LOG(LS_ERROR)
2033 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
2034
2035 *valid = false;
2036 return false;
2037 }
2038
2039 cricket::ContentInfo content =
2040 current_remote_desc->description()->contents()[mediacontent_index];
deadbeefcbecd352015-09-23 11:50:27 -07002041 cricket::BaseChannel* channel = GetChannel(content.name);
2042 if (!channel) {
2043 return false;
2044 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002045
deadbeefd59daf82015-10-14 15:02:44 -07002046 return transport_controller_->ReadyForRemoteCandidates(
deadbeefcbecd352015-09-23 11:50:27 -07002047 channel->transport_name());
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002048}
2049
deadbeefcbecd352015-09-23 11:50:27 -07002050void WebRtcSession::OnTransportControllerGatheringState(
2051 cricket::IceGatheringState state) {
2052 ASSERT(signaling_thread()->IsCurrent());
2053 if (state == cricket::kIceGatheringGathering) {
2054 if (ice_observer_) {
2055 ice_observer_->OnIceGatheringChange(
2056 PeerConnectionInterface::kIceGatheringGathering);
2057 }
2058 } else if (state == cricket::kIceGatheringComplete) {
2059 if (ice_observer_) {
2060 ice_observer_->OnIceGatheringChange(
2061 PeerConnectionInterface::kIceGatheringComplete);
deadbeefcbecd352015-09-23 11:50:27 -07002062 }
2063 }
2064}
2065
2066void WebRtcSession::ReportTransportStats() {
2067 // Use a set so we don't report the same stats twice if two channels share
2068 // a transport.
2069 std::set<std::string> transport_names;
2070 if (voice_channel()) {
2071 transport_names.insert(voice_channel()->transport_name());
2072 }
2073 if (video_channel()) {
2074 transport_names.insert(video_channel()->transport_name());
2075 }
2076 if (data_channel()) {
2077 transport_names.insert(data_channel()->transport_name());
2078 }
2079 for (const auto& name : transport_names) {
2080 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002081 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002082 ReportBestConnectionState(stats);
2083 ReportNegotiatedCiphers(stats);
2084 }
2085 }
2086}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002087// Walk through the ConnectionInfos to gather best connection usage
2088// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002089void WebRtcSession::ReportBestConnectionState(
2090 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002091 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002092 for (cricket::TransportChannelStatsList::const_iterator it =
2093 stats.channel_stats.begin();
2094 it != stats.channel_stats.end(); ++it) {
2095 for (cricket::ConnectionInfos::const_iterator it_info =
2096 it->connection_infos.begin();
2097 it_info != it->connection_infos.end(); ++it_info) {
2098 if (!it_info->best_connection) {
2099 continue;
2100 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002101
2102 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2103 const cricket::Candidate& local = it_info->local_candidate;
2104 const cricket::Candidate& remote = it_info->remote_candidate;
2105
2106 // Increment the counter for IceCandidatePairType.
2107 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2108 (local.type() == RELAY_PORT_TYPE &&
2109 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2110 type = kEnumCounterIceCandidatePairTypeTcp;
2111 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2112 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002113 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002114 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002115 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002116 metrics_observer_->IncrementEnumCounter(
2117 type, GetIceCandidatePairCounter(local, remote),
2118 kIceCandidatePairMax);
2119
2120 // Increment the counter for IP type.
2121 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002122 metrics_observer_->IncrementEnumCounter(
2123 kEnumCounterAddressFamily, kBestConnections_IPv4,
2124 kPeerConnectionAddressFamilyCounter_Max);
2125
2126 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002127 metrics_observer_->IncrementEnumCounter(
2128 kEnumCounterAddressFamily, kBestConnections_IPv6,
2129 kPeerConnectionAddressFamilyCounter_Max);
2130 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002131 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002132 }
2133
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002134 return;
2135 }
2136 }
2137}
2138
jbauchac8869e2015-07-03 01:36:14 -07002139void WebRtcSession::ReportNegotiatedCiphers(
2140 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002141 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002142 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2143 return;
2144 }
2145
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002146 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2147 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2148 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2149 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
jbauchac8869e2015-07-03 01:36:14 -07002150 return;
2151 }
2152
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002153 PeerConnectionEnumCounterType srtp_counter_type;
2154 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002155 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002156 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2157 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002158 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002159 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2160 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002161 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002162 srtp_counter_type = kEnumCounterDataSrtpCipher;
2163 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002164 } else {
2165 RTC_NOTREACHED();
2166 return;
2167 }
2168
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002169 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2170 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2171 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002172 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002173 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2174 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2175 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002176 }
2177}
2178
stefanc1aeaf02015-10-15 07:26:07 -07002179void WebRtcSession::OnSentPacket_w(cricket::TransportChannel* channel,
2180 const rtc::SentPacket& sent_packet) {
2181 RTC_DCHECK(worker_thread()->IsCurrent());
2182 media_controller_->call_w()->OnSentPacket(sent_packet);
2183}
2184
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002185} // namespace webrtc