blob: a3ff934dbc00c244dd4453922fe73dad68c9a431 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/app/webrtc/webrtcsession.h"
29
pbos@webrtc.org371243d2014-03-07 15:22:04 +000030#include <limits.h>
31
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032#include <algorithm>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033#include <vector>
34
35#include "talk/app/webrtc/jsepicecandidate.h"
36#include "talk/app/webrtc/jsepsessiondescription.h"
37#include "talk/app/webrtc/mediaconstraintsinterface.h"
38#include "talk/app/webrtc/mediastreamsignaling.h"
39#include "talk/app/webrtc/peerconnectioninterface.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000040#include "talk/app/webrtc/webrtcsessiondescriptionfactory.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041#include "talk/media/base/constants.h"
42#include "talk/media/base/videocapturer.h"
43#include "talk/session/media/channel.h"
44#include "talk/session/media/channelmanager.h"
45#include "talk/session/media/mediasession.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000046#include "webrtc/base/basictypes.h"
jbauchac8869e2015-07-03 01:36:14 -070047#include "webrtc/base/checks.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000048#include "webrtc/base/helpers.h"
49#include "webrtc/base/logging.h"
50#include "webrtc/base/stringencode.h"
51#include "webrtc/base/stringutils.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000052#include "webrtc/p2p/base/portallocator.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053
54using cricket::ContentInfo;
55using cricket::ContentInfos;
56using cricket::MediaContentDescription;
57using cricket::SessionDescription;
58using cricket::TransportInfo;
59
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060namespace webrtc {
61
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000063const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
64 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000065const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066const char kInvalidCandidates[] = "Description contains invalid candidates.";
67const char kInvalidSdp[] = "Invalid session description.";
68const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000069 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
70const char kPushDownTDFailed[] =
71 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000072const char kSdpWithoutDtlsFingerprint[] =
73 "Called with SDP without DTLS fingerprint.";
74const char kSdpWithoutSdesCrypto[] =
75 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000076const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000077 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000079const char kSessionErrorDesc[] = "Session error description: ";
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000080const char kDtlsSetupFailureRtp[] =
81 "Couldn't set up DTLS-SRTP on RTP channel.";
82const char kDtlsSetupFailureRtcp[] =
83 "Couldn't set up DTLS-SRTP on RTCP channel.";
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +000084const int kMaxUnsignalledRecvStreams = 20;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085
86// Compares |answer| against |offer|. Comparision is done
87// for number of m-lines in answer against offer. If matches true will be
88// returned otherwise false.
89static bool VerifyMediaDescriptions(
90 const SessionDescription* answer, const SessionDescription* offer) {
91 if (offer->contents().size() != answer->contents().size())
92 return false;
93
94 for (size_t i = 0; i < offer->contents().size(); ++i) {
95 if ((offer->contents()[i].name) != answer->contents()[i].name) {
96 return false;
97 }
wu@webrtc.org4e393072014-04-07 17:04:35 +000098 const MediaContentDescription* offer_mdesc =
99 static_cast<const MediaContentDescription*>(
100 offer->contents()[i].description);
101 const MediaContentDescription* answer_mdesc =
102 static_cast<const MediaContentDescription*>(
103 answer->contents()[i].description);
104 if (offer_mdesc->type() != answer_mdesc->type()) {
105 return false;
106 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107 }
108 return true;
109}
110
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111// Checks that each non-rejected content has SDES crypto keys or a DTLS
112// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
113// keys, will be caught in Transport negotiation, and backstopped by Channel's
114// |secure_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000115static bool VerifyCrypto(const SessionDescription* desc,
116 bool dtls_enabled,
117 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118 const ContentInfos& contents = desc->contents();
119 for (size_t index = 0; index < contents.size(); ++index) {
120 const ContentInfo* cinfo = &contents[index];
121 if (cinfo->rejected) {
122 continue;
123 }
124
125 // If the content isn't rejected, crypto must be present.
126 const MediaContentDescription* media =
127 static_cast<const MediaContentDescription*>(cinfo->description);
128 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
129 if (!media || !tinfo) {
130 // Something is not right.
131 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000132 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 return false;
134 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000135 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000136 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000137 LOG(LS_WARNING) <<
138 "Session description must have DTLS fingerprint if DTLS enabled.";
139 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000140 return false;
141 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000142 } else {
143 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000144 LOG(LS_WARNING) <<
145 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000146 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000147 return false;
148 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149 }
150 }
151
152 return true;
153}
154
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000155// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
156static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
157 const ContentInfos& contents = desc->contents();
158 for (size_t index = 0; index < contents.size(); ++index) {
159 const ContentInfo* cinfo = &contents[index];
160 if (cinfo->rejected) {
161 continue;
162 }
163
164 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
165 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
166 if (!tinfo) {
167 // Something is not right.
168 LOG(LS_ERROR) << kInvalidSdp;
169 return false;
170 }
171 if (tinfo->description.ice_ufrag.empty() ||
172 tinfo->description.ice_pwd.empty()) {
173 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
174 return false;
175 }
176 }
177 return true;
178}
179
wu@webrtc.org91053e72013-08-10 07:18:04 +0000180// Forces |sdesc->crypto_required| to the appropriate state based on the
181// current security policy, to ensure a failure occurs if there is an error
182// in crypto negotiation.
183// Called when processing the local session description.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000184static void UpdateSessionDescriptionSecurePolicy(cricket::CryptoType type,
185 SessionDescription* sdesc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000186 if (!sdesc) {
187 return;
188 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189
wu@webrtc.org91053e72013-08-10 07:18:04 +0000190 // Updating the |crypto_required_| in MediaContentDescription to the
191 // appropriate state based on the current security policy.
192 for (cricket::ContentInfos::iterator iter = sdesc->contents().begin();
193 iter != sdesc->contents().end(); ++iter) {
194 if (cricket::IsMediaContent(&*iter)) {
195 MediaContentDescription* mdesc =
196 static_cast<MediaContentDescription*> (iter->description);
197 if (mdesc) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000198 mdesc->set_crypto_required(type);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000199 }
200 }
201 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202}
203
204static bool GetAudioSsrcByTrackId(
205 const SessionDescription* session_description,
206 const std::string& track_id, uint32 *ssrc) {
207 const cricket::ContentInfo* audio_info =
208 cricket::GetFirstAudioContent(session_description);
209 if (!audio_info) {
210 LOG(LS_ERROR) << "Audio not used in this call";
211 return false;
212 }
213
214 const cricket::MediaContentDescription* audio_content =
215 static_cast<const cricket::MediaContentDescription*>(
216 audio_info->description);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000217 const cricket::StreamParams* stream =
218 cricket::GetStreamByIds(audio_content->streams(), "", track_id);
219 if (!stream) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220 return false;
221 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000222
223 *ssrc = stream->first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224 return true;
225}
226
227static bool GetTrackIdBySsrc(const SessionDescription* session_description,
228 uint32 ssrc, std::string* track_id) {
229 ASSERT(track_id != NULL);
230
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231 const cricket::ContentInfo* audio_info =
232 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000233 if (audio_info) {
234 const cricket::MediaContentDescription* audio_content =
235 static_cast<const cricket::MediaContentDescription*>(
236 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000237
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000238 const auto* found =
239 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
240 if (found) {
241 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000242 return true;
243 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244 }
245
246 const cricket::ContentInfo* video_info =
247 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000248 if (video_info) {
249 const cricket::MediaContentDescription* video_content =
250 static_cast<const cricket::MediaContentDescription*>(
251 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000253 const auto* found =
254 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
255 if (found) {
256 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000257 return true;
258 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259 }
260 return false;
261}
262
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000263static bool BadSdp(const std::string& source,
264 const std::string& type,
265 const std::string& reason,
266 std::string* err_desc) {
267 std::ostringstream desc;
268 desc << "Failed to set " << source << " " << type << " sdp: " << reason;
269
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000271 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000273 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 return false;
275}
276
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000278 const std::string& type,
279 const std::string& reason,
280 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000282 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000284 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285 }
286}
287
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000288static bool BadLocalSdp(const std::string& type,
289 const std::string& reason,
290 std::string* err_desc) {
291 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
292}
293
294static bool BadRemoteSdp(const std::string& type,
295 const std::string& reason,
296 std::string* err_desc) {
297 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
298}
299
300static bool BadOfferSdp(cricket::ContentSource source,
301 const std::string& reason,
302 std::string* err_desc) {
303 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
304}
305
306static bool BadPranswerSdp(cricket::ContentSource source,
307 const std::string& reason,
308 std::string* err_desc) {
309 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
310 reason, err_desc);
311}
312
313static bool BadAnswerSdp(cricket::ContentSource source,
314 const std::string& reason,
315 std::string* err_desc) {
316 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000317}
318
319#define GET_STRING_OF_STATE(state) \
320 case cricket::BaseSession::state: \
321 result = #state; \
322 break;
323
324static std::string GetStateString(cricket::BaseSession::State state) {
325 std::string result;
326 switch (state) {
327 GET_STRING_OF_STATE(STATE_INIT)
328 GET_STRING_OF_STATE(STATE_SENTINITIATE)
329 GET_STRING_OF_STATE(STATE_RECEIVEDINITIATE)
330 GET_STRING_OF_STATE(STATE_SENTPRACCEPT)
331 GET_STRING_OF_STATE(STATE_SENTACCEPT)
332 GET_STRING_OF_STATE(STATE_RECEIVEDPRACCEPT)
333 GET_STRING_OF_STATE(STATE_RECEIVEDACCEPT)
334 GET_STRING_OF_STATE(STATE_SENTMODIFY)
335 GET_STRING_OF_STATE(STATE_RECEIVEDMODIFY)
336 GET_STRING_OF_STATE(STATE_SENTREJECT)
337 GET_STRING_OF_STATE(STATE_RECEIVEDREJECT)
338 GET_STRING_OF_STATE(STATE_SENTREDIRECT)
339 GET_STRING_OF_STATE(STATE_SENTTERMINATE)
340 GET_STRING_OF_STATE(STATE_RECEIVEDTERMINATE)
341 GET_STRING_OF_STATE(STATE_INPROGRESS)
342 GET_STRING_OF_STATE(STATE_DEINIT)
343 default:
344 ASSERT(false);
345 break;
346 }
347 return result;
348}
349
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000350#define GET_STRING_OF_ERROR_CODE(err) \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000351 case cricket::BaseSession::err: \
352 result = #err; \
353 break;
354
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000355static std::string GetErrorCodeString(cricket::BaseSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356 std::string result;
357 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000358 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
359 GET_STRING_OF_ERROR_CODE(ERROR_TIME)
360 GET_STRING_OF_ERROR_CODE(ERROR_RESPONSE)
361 GET_STRING_OF_ERROR_CODE(ERROR_NETWORK)
362 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
363 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364 default:
365 ASSERT(false);
366 break;
367 }
368 return result;
369}
370
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000371static std::string MakeErrorString(const std::string& error,
372 const std::string& desc) {
373 std::ostringstream ret;
374 ret << error << " " << desc;
375 return ret.str();
376}
377
378static std::string MakeTdErrorString(const std::string& desc) {
379 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000380}
381
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000382// Set |option| to the highest-priority value of |key| in the optional
383// constraints if the key is found and has a valid value.
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000384template<typename T>
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000385static void SetOptionFromOptionalConstraint(
386 const MediaConstraintsInterface* constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000387 const std::string& key, cricket::Settable<T>* option) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000388 if (!constraints) {
389 return;
390 }
391 std::string string_value;
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000392 T value;
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000393 if (constraints->GetOptional().FindFirst(key, &string_value)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000394 if (rtc::FromString(string_value, &value)) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000395 option->Set(value);
396 }
397 }
398}
399
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000400uint32 ConvertIceTransportTypeToCandidateFilter(
401 PeerConnectionInterface::IceTransportsType type) {
402 switch (type) {
403 case PeerConnectionInterface::kNone:
404 return cricket::CF_NONE;
405 case PeerConnectionInterface::kRelay:
406 return cricket::CF_RELAY;
407 case PeerConnectionInterface::kNoHost:
408 return (cricket::CF_ALL & ~cricket::CF_HOST);
409 case PeerConnectionInterface::kAll:
410 return cricket::CF_ALL;
411 default: ASSERT(false);
412 }
413 return cricket::CF_NONE;
414}
415
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416// Help class used to remember if a a remote peer has requested ice restart by
417// by sending a description with new ice ufrag and password.
418class IceRestartAnswerLatch {
419 public:
420 IceRestartAnswerLatch() : ice_restart_(false) { }
421
wu@webrtc.org91053e72013-08-10 07:18:04 +0000422 // Returns true if CheckForRemoteIceRestart has been called with a new session
423 // description where ice password and ufrag has changed since last time
424 // Reset() was called.
425 bool Get() const {
426 return ice_restart_;
427 }
428
429 void Reset() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430 if (ice_restart_) {
431 ice_restart_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000432 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000433 }
434
435 void CheckForRemoteIceRestart(
436 const SessionDescriptionInterface* old_desc,
437 const SessionDescriptionInterface* new_desc) {
438 if (!old_desc || new_desc->type() != SessionDescriptionInterface::kOffer) {
439 return;
440 }
441 const SessionDescription* new_sd = new_desc->description();
442 const SessionDescription* old_sd = old_desc->description();
443 const ContentInfos& contents = new_sd->contents();
444 for (size_t index = 0; index < contents.size(); ++index) {
445 const ContentInfo* cinfo = &contents[index];
446 if (cinfo->rejected) {
447 continue;
448 }
449 // If the content isn't rejected, check if ufrag and password has
450 // changed.
451 const cricket::TransportDescription* new_transport_desc =
452 new_sd->GetTransportDescriptionByName(cinfo->name);
453 const cricket::TransportDescription* old_transport_desc =
454 old_sd->GetTransportDescriptionByName(cinfo->name);
455 if (!new_transport_desc || !old_transport_desc) {
456 // No transport description exist. This is not an ice restart.
457 continue;
458 }
jiayl@webrtc.orgdb397e52014-06-20 16:32:09 +0000459 if (cricket::IceCredentialsChanged(old_transport_desc->ice_ufrag,
460 old_transport_desc->ice_pwd,
461 new_transport_desc->ice_ufrag,
462 new_transport_desc->ice_pwd)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463 LOG(LS_INFO) << "Remote peer request ice restart.";
464 ice_restart_ = true;
465 break;
466 }
467 }
468 }
469
470 private:
471 bool ice_restart_;
472};
473
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000474WebRtcSession::WebRtcSession(
475 cricket::ChannelManager* channel_manager,
476 rtc::Thread* signaling_thread,
477 rtc::Thread* worker_thread,
478 cricket::PortAllocator* port_allocator,
479 MediaStreamSignaling* mediastream_signaling)
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000480 : cricket::BaseSession(signaling_thread,
481 worker_thread,
482 port_allocator,
483 rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX),
484 cricket::NS_JINGLE_RTP,
485 false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486 // RFC 3264: The numeric value of the session id and version in the
487 // o line MUST be representable with a "64 bit signed integer".
488 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
489 channel_manager_(channel_manager),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490 mediastream_signaling_(mediastream_signaling),
491 ice_observer_(NULL),
492 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700493 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000495 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000497 ice_restart_latch_(new IceRestartAnswerLatch),
498 metrics_observer_(NULL) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499}
500
501WebRtcSession::~WebRtcSession() {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000502 // Destroy video_channel_ first since it may have a pointer to the
503 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000504 if (video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505 SignalVideoChannelDestroyed();
506 channel_manager_->DestroyVideoChannel(video_channel_.release());
507 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000508 if (voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000509 SignalVoiceChannelDestroyed();
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200510 channel_manager_->DestroyVoiceChannel(voice_channel_.release(), nullptr);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000511 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000512 if (data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513 SignalDataChannelDestroyed();
514 channel_manager_->DestroyDataChannel(data_channel_.release());
515 }
516 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
517 delete saved_candidates_[i];
518 }
519 delete identity();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520}
521
wu@webrtc.org91053e72013-08-10 07:18:04 +0000522bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000523 const PeerConnectionFactoryInterface::Options& options,
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000524 const MediaConstraintsInterface* constraints,
525 DTLSIdentityServiceInterface* dtls_identity_service,
Henrik Lundin64dad832015-05-11 12:44:23 +0200526 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
527 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700528 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
Joachim Bauch04e5b492015-05-29 09:40:39 +0200529 SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000530
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531 // TODO(perkj): Take |constraints| into consideration. Return false if not all
532 // mandatory constraints can be fulfilled. Note that |constraints|
533 // can be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534 bool value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000535
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000536 if (options.disable_encryption) {
537 dtls_enabled_ = false;
538 } else {
539 // Enable DTLS by default if |dtls_identity_service| is valid.
540 dtls_enabled_ = (dtls_identity_service != NULL);
541 // |constraints| can override the default |dtls_enabled_| value.
542 if (FindConstraint(
543 constraints,
544 MediaConstraintsInterface::kEnableDtlsSrtp,
545 &value, NULL)) {
546 dtls_enabled_ = value;
547 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000548 }
549
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000551 // It takes precendence over the disable_sctp_data_channels
552 // PeerConnectionFactoryInterface::Options.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 if (FindConstraint(
554 constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
555 &value, NULL) && value) {
556 LOG(LS_INFO) << "Allowing RTP data engine.";
557 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000558 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000559 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000560 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000561 LOG(LS_INFO) << "Allowing SCTP data engine.";
562 data_channel_type_ = cricket::DCT_SCTP;
563 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564 }
565 if (data_channel_type_ != cricket::DCT_NONE) {
566 mediastream_signaling_->SetDataChannelFactory(this);
567 }
568
wu@webrtc.orgde305012013-10-31 15:40:38 +0000569 // Find DSCP constraint.
570 if (FindConstraint(
571 constraints,
572 MediaConstraintsInterface::kEnableDscp,
573 &value, NULL)) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000574 audio_options_.dscp.Set(value);
575 video_options_.dscp.Set(value);
576 }
577
578 // Find Suspend Below Min Bitrate constraint.
579 if (FindConstraint(
580 constraints,
581 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
582 &value,
583 NULL)) {
584 video_options_.suspend_below_min_bitrate.Set(value);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000585 }
586
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000587 SetOptionFromOptionalConstraint(constraints,
588 MediaConstraintsInterface::kScreencastMinBitrate,
589 &video_options_.screencast_min_bitrate);
590
591 // Find constraints for cpu overuse detection.
592 SetOptionFromOptionalConstraint(constraints,
593 MediaConstraintsInterface::kCpuUnderuseThreshold,
594 &video_options_.cpu_underuse_threshold);
595 SetOptionFromOptionalConstraint(constraints,
596 MediaConstraintsInterface::kCpuOveruseThreshold,
597 &video_options_.cpu_overuse_threshold);
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000598 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000599 MediaConstraintsInterface::kCpuOveruseDetection,
600 &video_options_.cpu_overuse_detection);
601 SetOptionFromOptionalConstraint(constraints,
602 MediaConstraintsInterface::kCpuOveruseEncodeUsage,
603 &video_options_.cpu_overuse_encode_usage);
604 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000605 MediaConstraintsInterface::kCpuUnderuseEncodeRsdThreshold,
606 &video_options_.cpu_underuse_encode_rsd_threshold);
607 SetOptionFromOptionalConstraint(constraints,
608 MediaConstraintsInterface::kCpuOveruseEncodeRsdThreshold,
609 &video_options_.cpu_overuse_encode_rsd_threshold);
buildbot@webrtc.orgdb563902014-06-13 13:05:48 +0000610
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000611 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000612 MediaConstraintsInterface::kNumUnsignalledRecvStreams,
613 &video_options_.unsignalled_recv_stream_limit);
614 if (video_options_.unsignalled_recv_stream_limit.IsSet()) {
615 int stream_limit;
616 video_options_.unsignalled_recv_stream_limit.Get(&stream_limit);
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000617 stream_limit = std::min(kMaxUnsignalledRecvStreams, stream_limit);
618 stream_limit = std::max(0, stream_limit);
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000619 video_options_.unsignalled_recv_stream_limit.Set(stream_limit);
620 }
621
622 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000623 MediaConstraintsInterface::kHighStartBitrate,
624 &video_options_.video_start_bitrate);
wu@webrtc.orgcfe5e9c2014-03-27 17:03:58 +0000625
626 if (FindConstraint(
627 constraints,
628 MediaConstraintsInterface::kVeryHighBitrate,
629 &value,
630 NULL)) {
631 video_options_.video_highest_bitrate.Set(
632 cricket::VideoOptions::VERY_HIGH);
633 } else if (FindConstraint(
634 constraints,
635 MediaConstraintsInterface::kHighBitrate,
636 &value,
637 NULL)) {
638 video_options_.video_highest_bitrate.Set(
639 cricket::VideoOptions::HIGH);
640 }
641
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000642 SetOptionFromOptionalConstraint(constraints,
643 MediaConstraintsInterface::kCombinedAudioVideoBwe,
644 &audio_options_.combined_audio_video_bwe);
645
Henrik Lundin64dad832015-05-11 12:44:23 +0200646 audio_options_.audio_jitter_buffer_max_packets.Set(
647 rtc_configuration.audio_jitter_buffer_max_packets);
648
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200649 audio_options_.audio_jitter_buffer_fast_accelerate.Set(
650 rtc_configuration.audio_jitter_buffer_fast_accelerate);
651
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652 const cricket::VideoCodec default_codec(
653 JsepSessionDescription::kDefaultVideoCodecId,
654 JsepSessionDescription::kDefaultVideoCodecName,
655 JsepSessionDescription::kMaxVideoCodecWidth,
656 JsepSessionDescription::kMaxVideoCodecHeight,
657 JsepSessionDescription::kDefaultVideoCodecFramerate,
658 JsepSessionDescription::kDefaultVideoCodecPreference);
659 channel_manager_->SetDefaultVideoEncoderConfig(
660 cricket::VideoEncoderConfig(default_codec));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000661
662 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
663 signaling_thread(),
664 channel_manager_,
665 mediastream_signaling_,
666 dtls_identity_service,
667 this,
668 id(),
669 data_channel_type_,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000670 dtls_enabled_));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000671
672 webrtc_session_desc_factory_->SignalIdentityReady.connect(
673 this, &WebRtcSession::OnIdentityReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000674
wu@webrtc.org97077a32013-10-25 21:18:33 +0000675 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000676 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000677 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000678 port_allocator()->set_candidate_filter(
Henrik Lundin64dad832015-05-11 12:44:23 +0200679 ConvertIceTransportTypeToCandidateFilter(rtc_configuration.type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000680 return true;
681}
682
683void WebRtcSession::Terminate() {
684 SetState(STATE_RECEIVEDTERMINATE);
685 RemoveUnusedChannelsAndTransports(NULL);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000686 ASSERT(!voice_channel_);
687 ASSERT(!video_channel_);
688 ASSERT(!data_channel_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689}
690
691bool WebRtcSession::StartCandidatesAllocation() {
692 // SpeculativelyConnectTransportChannels, will call ConnectChannels method
693 // from TransportProxy to start gathering ice candidates.
694 SpeculativelyConnectAllTransportChannels();
695 if (!saved_candidates_.empty()) {
696 // If there are saved candidates which arrived before local description is
697 // set, copy those to remote description.
698 CopySavedCandidates(remote_desc_.get());
699 }
700 // Push remote candidates present in remote description to transport channels.
701 UseCandidatesInSessionDescription(remote_desc_.get());
702 return true;
703}
704
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000705void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
706 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707}
708
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000709cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
710 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711}
712
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000713bool WebRtcSession::GetSslRole(rtc::SSLRole* role) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000714 if (local_description() == NULL || remote_description() == NULL) {
715 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
716 << "SSL Role of the session.";
717 return false;
718 }
719
720 // TODO(mallinath) - Return role of each transport, as role may differ from
721 // one another.
722 // In current implementaion we just return the role of first transport in the
723 // transport map.
724 for (cricket::TransportMap::const_iterator iter = transport_proxies().begin();
725 iter != transport_proxies().end(); ++iter) {
726 if (iter->second->impl()) {
727 return iter->second->impl()->GetSslRole(role);
728 }
729 }
730 return false;
731}
732
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000733void WebRtcSession::CreateOffer(
734 CreateSessionDescriptionObserver* observer,
735 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
736 webrtc_session_desc_factory_->CreateOffer(observer, options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000737}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000738
wu@webrtc.org91053e72013-08-10 07:18:04 +0000739void WebRtcSession::CreateAnswer(CreateSessionDescriptionObserver* observer,
740 const MediaConstraintsInterface* constraints) {
741 webrtc_session_desc_factory_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000742}
743
744bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
745 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000746 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000747 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000748
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000749 // Validate SDP.
750 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
751 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000752 }
753
754 // Update the initiator flag if this session is the initiator.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000755 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756 if (state() == STATE_INIT && action == kOffer) {
757 set_initiator(true);
758 }
759
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000760 cricket::SecurePolicy sdes_policy =
761 webrtc_session_desc_factory_->SdesPolicy();
762 cricket::CryptoType crypto_required = dtls_enabled_ ?
763 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
764 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000766 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767
768 set_local_description(desc->description()->Copy());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000769 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770
771 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000772 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 // TODO(mallinath) - Handle CreateChannel failure, as new local description
774 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000775 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776 }
777
778 // Remove channel and transport proxies, if MediaContentDescription is
779 // rejected.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000780 RemoveUnusedChannelsAndTransports(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000782 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 return false;
784 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000785
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786 // Kick starting the ice candidates allocation.
787 StartCandidatesAllocation();
788
789 // Update state and SSRC of local MediaStreams and DataChannels based on the
790 // local session description.
791 mediastream_signaling_->OnLocalDescriptionChanged(local_desc_.get());
792
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000793 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000794 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
795 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
796 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000798 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799 }
800 return true;
801}
802
803bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
804 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000805 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000806 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000807
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000808 // Validate SDP.
809 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
810 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 }
812
813 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000814 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 if (action == kOffer && !CreateChannels(desc->description())) {
816 // TODO(mallinath) - Handle CreateChannel failure, as new local description
817 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000818 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819 }
820
821 // Remove channel and transport proxies, if MediaContentDescription is
822 // rejected.
823 RemoveUnusedChannelsAndTransports(desc->description());
824
825 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
826 // is called.
827 set_remote_description(desc->description()->Copy());
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000828 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000829 return false;
830 }
831
832 // Update remote MediaStreams.
833 mediastream_signaling_->OnRemoteDescriptionChanged(desc);
834 if (local_description() && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000835 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 }
837
838 // Copy all saved candidates.
839 CopySavedCandidates(desc);
840 // We retain all received candidates.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000841 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
842 remote_desc_.get(), desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843 // Check if this new SessionDescription contains new ice ufrag and password
844 // that indicates the remote peer requests ice restart.
845 ice_restart_latch_->CheckForRemoteIceRestart(remote_desc_.get(),
846 desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000847 remote_desc_.reset(desc_temp.release());
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000848
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000849 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000850 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
851 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
852 }
853
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000855 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000856 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000857
858 // Set the the ICE connection state to connecting since the connection may
859 // become writable with peer reflexive candidates before any remote candidate
860 // is signaled.
861 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
862 // is to have a new signal the indicates a change in checking state from the
863 // transport and expose a new checking() member from transport that can be
864 // read to determine the current checking state. The existing SignalConnecting
865 // actually means "gathering candidates", so cannot be be used here.
866 if (desc->type() != SessionDescriptionInterface::kOffer &&
867 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
868 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
869 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870 return true;
871}
872
873bool WebRtcSession::UpdateSessionState(
874 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 std::string* err_desc) {
876 // If there's already a pending error then no state transition should happen.
877 // But all call-sites should be verifying this before calling us!
878 ASSERT(error() == cricket::BaseSession::ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000879 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000881 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
882 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 }
884 SetState(source == cricket::CS_LOCAL ?
885 STATE_SENTINITIATE : STATE_RECEIVEDINITIATE);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000886 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
887 SetError(BaseSession::ERROR_CONTENT, *err_desc);
888 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000890 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891 }
892 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000893 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
894 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000895 }
896 EnableChannels();
897 SetState(source == cricket::CS_LOCAL ?
898 STATE_SENTPRACCEPT : STATE_RECEIVEDPRACCEPT);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000899 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
900 SetError(BaseSession::ERROR_CONTENT, *err_desc);
901 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000903 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904 }
905 } else if (action == kAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000906 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
907 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 }
909 MaybeEnableMuxingSupport();
910 EnableChannels();
911 SetState(source == cricket::CS_LOCAL ?
912 STATE_SENTACCEPT : STATE_RECEIVEDACCEPT);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000913 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
914 SetError(BaseSession::ERROR_CONTENT, *err_desc);
915 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000917 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000918 }
919 }
920 return true;
921}
922
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000923bool WebRtcSession::PushdownMediaDescription(
924 cricket::ContentAction action,
925 cricket::ContentSource source,
926 std::string* err) {
927 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
928 if (!ch) {
929 return true;
930 } else if (source == cricket::CS_LOCAL) {
931 return ch->PushdownLocalDescription(
932 base_local_description(), action, err);
933 } else {
934 return ch->PushdownRemoteDescription(
935 base_remote_description(), action, err);
936 }
937 };
938
939 return (set_content(voice_channel()) &&
940 set_content(video_channel()) &&
941 set_content(data_channel()));
942}
943
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
945 if (type == SessionDescriptionInterface::kOffer) {
946 return WebRtcSession::kOffer;
947 } else if (type == SessionDescriptionInterface::kPrAnswer) {
948 return WebRtcSession::kPrAnswer;
949 } else if (type == SessionDescriptionInterface::kAnswer) {
950 return WebRtcSession::kAnswer;
951 }
952 ASSERT(false && "unknown action type");
953 return WebRtcSession::kOffer;
954}
955
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000956bool WebRtcSession::GetTransportStats(cricket::SessionStats* stats) {
957 ASSERT(signaling_thread()->IsCurrent());
958
959 const auto get_transport_stats = [stats](const std::string& content_name,
960 cricket::Transport* transport) {
961 const std::string& transport_id = transport->content_name();
962 stats->proxy_to_transport[content_name] = transport_id;
963 if (stats->transport_stats.find(transport_id)
964 != stats->transport_stats.end()) {
965 // Transport stats already done for this transport.
966 return true;
967 }
968
969 cricket::TransportStats tstats;
970 if (!transport->GetStats(&tstats)) {
971 return false;
972 }
973
974 stats->transport_stats[transport_id] = tstats;
975 return true;
976 };
977
978 for (const auto& kv : transport_proxies()) {
979 cricket::Transport* transport = kv.second->impl();
980 if (transport && !get_transport_stats(kv.first, transport)) {
981 return false;
982 }
983 }
984 return true;
985}
986
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000987bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
988 if (state() == STATE_INIT) {
989 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
990 << "without any offer (local or remote) "
991 << "session description.";
992 return false;
993 }
994
995 if (!candidate) {
996 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL";
997 return false;
998 }
999
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001000 bool valid = false;
1001 if (!ReadyToUseRemoteCandidate(candidate, NULL, &valid)) {
1002 if (valid) {
1003 LOG(LS_INFO) << "ProcessIceMessage: Candidate saved";
1004 saved_candidates_.push_back(
1005 new JsepIceCandidate(candidate->sdp_mid(),
1006 candidate->sdp_mline_index(),
1007 candidate->candidate()));
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001008 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001009 return valid;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001010 }
1011
1012 // Add this candidate to the remote session description.
1013 if (!remote_desc_->AddCandidate(candidate)) {
1014 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used";
1015 return false;
1016 }
1017
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001018 return UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001019}
1020
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001021bool WebRtcSession::SetIceTransports(
1022 PeerConnectionInterface::IceTransportsType type) {
1023 return port_allocator()->set_candidate_filter(
1024 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001025}
1026
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001027bool WebRtcSession::GetLocalTrackIdBySsrc(uint32 ssrc, std::string* track_id) {
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001028 if (!base_local_description())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001029 return false;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001030 return webrtc::GetTrackIdBySsrc(base_local_description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001031}
1032
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001033bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32 ssrc, std::string* track_id) {
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001034 if (!base_remote_description())
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001035 return false;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001036 return webrtc::GetTrackIdBySsrc(base_remote_description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001037}
1038
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001039std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001040 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001041 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001042 return desc.str();
1043}
1044
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001045void WebRtcSession::SetAudioPlayout(uint32 ssrc, bool enable,
1046 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047 ASSERT(signaling_thread()->IsCurrent());
1048 if (!voice_channel_) {
1049 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
1050 return;
1051 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001052 if (!voice_channel_->SetRemoteRenderer(ssrc, renderer)) {
1053 // SetRenderer() can fail if the ssrc does not match any playout channel.
1054 LOG(LS_ERROR) << "SetAudioPlayout: ssrc is incorrect: " << ssrc;
1055 return;
1056 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001057 if (!voice_channel_->SetOutputScaling(ssrc, enable ? 1 : 0, enable ? 1 : 0)) {
1058 // Allow that SetOutputScaling fail if |enable| is false but assert
1059 // otherwise. This in the normal case when the underlying media channel has
1060 // already been deleted.
1061 ASSERT(enable == false);
1062 }
1063}
1064
1065void WebRtcSession::SetAudioSend(uint32 ssrc, bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001066 const cricket::AudioOptions& options,
1067 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001068 ASSERT(signaling_thread()->IsCurrent());
1069 if (!voice_channel_) {
1070 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
1071 return;
1072 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001073 if (!voice_channel_->SetLocalRenderer(ssrc, renderer)) {
1074 // SetRenderer() can fail if the ssrc does not match any send channel.
1075 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
1076 return;
1077 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001078 if (!voice_channel_->MuteStream(ssrc, !enable)) {
1079 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1080 // This in the normal case when the underlying media channel has already
1081 // been deleted.
1082 ASSERT(enable == false);
1083 return;
1084 }
1085 if (enable)
1086 voice_channel_->SetChannelOptions(options);
1087}
1088
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001089void WebRtcSession::SetAudioPlayoutVolume(uint32 ssrc, double volume) {
1090 ASSERT(signaling_thread()->IsCurrent());
1091 ASSERT(volume >= 0 && volume <= 10);
1092 if (!voice_channel_) {
1093 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1094 return;
1095 }
1096
1097 if (!voice_channel_->SetOutputScaling(ssrc, volume, volume))
1098 ASSERT(false);
1099}
1100
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001101bool WebRtcSession::SetCaptureDevice(uint32 ssrc,
1102 cricket::VideoCapturer* camera) {
1103 ASSERT(signaling_thread()->IsCurrent());
1104
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001105 if (!video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001106 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1107 // support video.
1108 LOG(LS_WARNING) << "Video not used in this call.";
1109 return false;
1110 }
1111 if (!video_channel_->SetCapturer(ssrc, camera)) {
1112 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1113 // This in the normal case when the underlying media channel has already
1114 // been deleted.
1115 ASSERT(camera == NULL);
1116 return false;
1117 }
1118 return true;
1119}
1120
1121void WebRtcSession::SetVideoPlayout(uint32 ssrc,
1122 bool enable,
1123 cricket::VideoRenderer* renderer) {
1124 ASSERT(signaling_thread()->IsCurrent());
1125 if (!video_channel_) {
1126 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1127 return;
1128 }
1129 if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) {
1130 // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise.
1131 // This in the normal case when the underlying media channel has already
1132 // been deleted.
1133 ASSERT(renderer == NULL);
1134 }
1135}
1136
1137void WebRtcSession::SetVideoSend(uint32 ssrc, bool enable,
1138 const cricket::VideoOptions* options) {
1139 ASSERT(signaling_thread()->IsCurrent());
1140 if (!video_channel_) {
1141 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1142 return;
1143 }
1144 if (!video_channel_->MuteStream(ssrc, !enable)) {
1145 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1146 // This in the normal case when the underlying media channel has already
1147 // been deleted.
1148 ASSERT(enable == false);
1149 return;
1150 }
1151 if (enable && options)
1152 video_channel_->SetChannelOptions(*options);
1153}
1154
1155bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1156 ASSERT(signaling_thread()->IsCurrent());
1157 if (!voice_channel_) {
1158 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1159 return false;
1160 }
1161 uint32 send_ssrc = 0;
1162 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1163 // exists.
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001164 if (!GetAudioSsrcByTrackId(base_local_description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001165 &send_ssrc)) {
1166 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1167 return false;
1168 }
1169 return voice_channel_->CanInsertDtmf();
1170}
1171
1172bool WebRtcSession::InsertDtmf(const std::string& track_id,
1173 int code, int duration) {
1174 ASSERT(signaling_thread()->IsCurrent());
1175 if (!voice_channel_) {
1176 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1177 return false;
1178 }
1179 uint32 send_ssrc = 0;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001180 if (!VERIFY(GetAudioSsrcByTrackId(base_local_description(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181 track_id, &send_ssrc))) {
1182 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1183 return false;
1184 }
1185 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration,
1186 cricket::DF_SEND)) {
1187 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1188 return false;
1189 }
1190 return true;
1191}
1192
1193sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
1194 return &SignalVoiceChannelDestroyed;
1195}
1196
wu@webrtc.org78187522013-10-07 23:32:02 +00001197bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001198 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001199 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001200 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001201 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1202 return false;
1203 }
1204 return data_channel_->SendData(params, payload, result);
1205}
1206
1207bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001208 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001209 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1210 return false;
1211 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001212 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1213 &DataChannel::OnChannelReady);
1214 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1215 &DataChannel::OnDataReceived);
wu@webrtc.org78187522013-10-07 23:32:02 +00001216 return true;
1217}
1218
1219void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001220 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001221 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1222 return;
1223 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001224 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1225 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
1226}
1227
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001228void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001229 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001230 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1231 return;
1232 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001233 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1234 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001235}
1236
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001237void WebRtcSession::RemoveSctpDataStream(int sid) {
1238 mediastream_signaling_->RemoveSctpDataChannel(sid);
jiayl@webrtc.org2eaac182014-06-17 16:02:46 +00001239
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001240 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001241 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1242 << "NULL.";
1243 return;
1244 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001245 data_channel_->RemoveRecvStream(sid);
1246 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001247}
1248
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001249bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001250 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001251}
1252
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001253rtc::scoped_refptr<DataChannel> WebRtcSession::CreateDataChannel(
wu@webrtc.org78187522013-10-07 23:32:02 +00001254 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001255 const InternalDataChannelInit* config) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001256 if (state() == STATE_RECEIVEDTERMINATE) {
1257 return NULL;
1258 }
1259 if (data_channel_type_ == cricket::DCT_NONE) {
1260 LOG(LS_ERROR) << "CreateDataChannel: Data is not supported in this call.";
1261 return NULL;
1262 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001263 InternalDataChannelInit new_config =
1264 config ? (*config) : InternalDataChannelInit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001265 if (data_channel_type_ == cricket::DCT_SCTP) {
1266 if (new_config.id < 0) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001267 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001268 if (GetSslRole(&role) &&
1269 !mediastream_signaling_->AllocateSctpSid(role, &new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001270 LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel.";
1271 return NULL;
1272 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001273 } else if (!mediastream_signaling_->IsSctpSidAvailable(new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001274 LOG(LS_ERROR) << "Failed to create a SCTP data channel "
1275 << "because the id is already in use or out of range.";
1276 return NULL;
1277 }
1278 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001279
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001280 rtc::scoped_refptr<DataChannel> channel(DataChannel::Create(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001281 this, data_channel_type_, label, new_config));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001282 if (channel && !mediastream_signaling_->AddDataChannel(channel))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001283 return NULL;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001284
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001285 return channel;
1286}
1287
1288cricket::DataChannelType WebRtcSession::data_channel_type() const {
1289 return data_channel_type_;
1290}
1291
wu@webrtc.org91053e72013-08-10 07:18:04 +00001292bool WebRtcSession::IceRestartPending() const {
1293 return ice_restart_latch_->Get();
1294}
1295
1296void WebRtcSession::ResetIceRestartLatch() {
1297 ice_restart_latch_->Reset();
1298}
1299
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001300void WebRtcSession::OnIdentityReady(rtc::SSLIdentity* identity) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001301 SetIdentity(identity);
1302}
1303
1304bool WebRtcSession::waiting_for_identity() const {
1305 return webrtc_session_desc_factory_->waiting_for_identity();
1306}
1307
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001308void WebRtcSession::SetIceConnectionState(
1309 PeerConnectionInterface::IceConnectionState state) {
1310 if (ice_connection_state_ == state) {
1311 return;
1312 }
1313
1314 // ASSERT that the requested transition is allowed. Note that
1315 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1316 // within PeerConnection). This switch statement should compile away when
1317 // ASSERTs are disabled.
1318 switch (ice_connection_state_) {
1319 case PeerConnectionInterface::kIceConnectionNew:
1320 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1321 break;
1322 case PeerConnectionInterface::kIceConnectionChecking:
1323 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1324 state == PeerConnectionInterface::kIceConnectionConnected);
1325 break;
1326 case PeerConnectionInterface::kIceConnectionConnected:
1327 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1328 state == PeerConnectionInterface::kIceConnectionChecking ||
1329 state == PeerConnectionInterface::kIceConnectionCompleted);
1330 break;
1331 case PeerConnectionInterface::kIceConnectionCompleted:
1332 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1333 state == PeerConnectionInterface::kIceConnectionDisconnected);
1334 break;
1335 case PeerConnectionInterface::kIceConnectionFailed:
1336 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1337 break;
1338 case PeerConnectionInterface::kIceConnectionDisconnected:
1339 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1340 state == PeerConnectionInterface::kIceConnectionConnected ||
1341 state == PeerConnectionInterface::kIceConnectionCompleted ||
1342 state == PeerConnectionInterface::kIceConnectionFailed);
1343 break;
1344 case PeerConnectionInterface::kIceConnectionClosed:
1345 ASSERT(false);
1346 break;
1347 default:
1348 ASSERT(false);
1349 break;
1350 }
1351
1352 ice_connection_state_ = state;
1353 if (ice_observer_) {
1354 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1355 }
1356}
1357
1358void WebRtcSession::OnTransportRequestSignaling(
1359 cricket::Transport* transport) {
1360 ASSERT(signaling_thread()->IsCurrent());
1361 transport->OnSignalingReady();
1362 if (ice_observer_) {
1363 ice_observer_->OnIceGatheringChange(
1364 PeerConnectionInterface::kIceGatheringGathering);
1365 }
1366}
1367
1368void WebRtcSession::OnTransportConnecting(cricket::Transport* transport) {
1369 ASSERT(signaling_thread()->IsCurrent());
1370 // start monitoring for the write state of the transport.
1371 OnTransportWritable(transport);
1372}
1373
1374void WebRtcSession::OnTransportWritable(cricket::Transport* transport) {
1375 ASSERT(signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001376 if (transport->all_channels_writable()) {
henrike@webrtc.org05376342014-03-10 15:53:12 +00001377 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001378 } else if (transport->HasChannels()) {
1379 // If the current state is Connected or Completed, then there were writable
1380 // channels but now there are not, so the next state must be Disconnected.
1381 if (ice_connection_state_ ==
1382 PeerConnectionInterface::kIceConnectionConnected ||
1383 ice_connection_state_ ==
1384 PeerConnectionInterface::kIceConnectionCompleted) {
1385 SetIceConnectionState(
1386 PeerConnectionInterface::kIceConnectionDisconnected);
1387 }
1388 }
1389}
1390
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001391void WebRtcSession::OnTransportCompleted(cricket::Transport* transport) {
1392 ASSERT(signaling_thread()->IsCurrent());
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001393 PeerConnectionInterface::IceConnectionState old_state = ice_connection_state_;
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001394 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001395 // Only report once when Ice connection is completed.
1396 if (old_state != PeerConnectionInterface::kIceConnectionCompleted) {
jbauchac8869e2015-07-03 01:36:14 -07001397 cricket::TransportStats stats;
1398 if (metrics_observer_ && transport->GetStats(&stats)) {
1399 ReportBestConnectionState(stats);
1400 ReportNegotiatedCiphers(stats);
1401 }
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001402 }
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001403}
1404
1405void WebRtcSession::OnTransportFailed(cricket::Transport* transport) {
1406 ASSERT(signaling_thread()->IsCurrent());
1407 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1408}
1409
Peter Thatcher54360512015-07-08 11:08:35 -07001410void WebRtcSession::OnTransportReceiving(cricket::Transport* transport) {
1411 ASSERT(signaling_thread()->IsCurrent());
1412 // The ice connection is considered receiving if at least one transport is
1413 // receiving on any channels.
1414 bool receiving = false;
1415 for (const auto& kv : transport_proxies()) {
1416 cricket::Transport* transport = kv.second->impl();
1417 if (transport && transport->any_channel_receiving()) {
1418 receiving = true;
1419 break;
1420 }
1421 }
1422 SetIceConnectionReceiving(receiving);
1423}
1424
1425void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1426 if (ice_connection_receiving_ == receiving) {
1427 return;
1428 }
1429 ice_connection_receiving_ = receiving;
1430 if (ice_observer_) {
1431 ice_observer_->OnIceConnectionReceivingChange(receiving);
1432 }
1433}
1434
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001435void WebRtcSession::OnTransportProxyCandidatesReady(
1436 cricket::TransportProxy* proxy, const cricket::Candidates& candidates) {
1437 ASSERT(signaling_thread()->IsCurrent());
1438 ProcessNewLocalCandidate(proxy->content_name(), candidates);
1439}
1440
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001441void WebRtcSession::OnCandidatesAllocationDone() {
1442 ASSERT(signaling_thread()->IsCurrent());
1443 if (ice_observer_) {
1444 ice_observer_->OnIceGatheringChange(
1445 PeerConnectionInterface::kIceGatheringComplete);
1446 ice_observer_->OnIceComplete();
1447 }
1448}
1449
1450// Enabling voice and video channel.
1451void WebRtcSession::EnableChannels() {
1452 if (voice_channel_ && !voice_channel_->enabled())
1453 voice_channel_->Enable(true);
1454
1455 if (video_channel_ && !video_channel_->enabled())
1456 video_channel_->Enable(true);
1457
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001458 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001459 data_channel_->Enable(true);
1460}
1461
1462void WebRtcSession::ProcessNewLocalCandidate(
1463 const std::string& content_name,
1464 const cricket::Candidates& candidates) {
1465 int sdp_mline_index;
1466 if (!GetLocalCandidateMediaIndex(content_name, &sdp_mline_index)) {
1467 LOG(LS_ERROR) << "ProcessNewLocalCandidate: content name "
1468 << content_name << " not found";
1469 return;
1470 }
1471
1472 for (cricket::Candidates::const_iterator citer = candidates.begin();
1473 citer != candidates.end(); ++citer) {
1474 // Use content_name as the candidate media id.
1475 JsepIceCandidate candidate(content_name, sdp_mline_index, *citer);
1476 if (ice_observer_) {
1477 ice_observer_->OnIceCandidate(&candidate);
1478 }
1479 if (local_desc_) {
1480 local_desc_->AddCandidate(&candidate);
1481 }
1482 }
1483}
1484
1485// Returns the media index for a local ice candidate given the content name.
1486bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1487 int* sdp_mline_index) {
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001488 if (!base_local_description() || !sdp_mline_index)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001489 return false;
1490
1491 bool content_found = false;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001492 const ContentInfos& contents = base_local_description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001493 for (size_t index = 0; index < contents.size(); ++index) {
1494 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001495 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001496 content_found = true;
1497 break;
1498 }
1499 }
1500 return content_found;
1501}
1502
1503bool WebRtcSession::UseCandidatesInSessionDescription(
1504 const SessionDescriptionInterface* remote_desc) {
1505 if (!remote_desc)
1506 return true;
1507 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001508
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001509 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1510 const IceCandidateCollection* candidates = remote_desc->candidates(m);
1511 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001512 const IceCandidateInterface* candidate = candidates->at(n);
1513 bool valid = false;
1514 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1515 if (valid) {
1516 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Candidate saved.";
1517 saved_candidates_.push_back(
1518 new JsepIceCandidate(candidate->sdp_mid(),
1519 candidate->sdp_mline_index(),
1520 candidate->candidate()));
1521 }
1522 continue;
1523 }
1524
1525 ret = UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001526 if (!ret)
1527 break;
1528 }
1529 }
1530 return ret;
1531}
1532
1533bool WebRtcSession::UseCandidate(
1534 const IceCandidateInterface* candidate) {
1535
1536 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001537 size_t remote_content_size = base_remote_description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001538 if (mediacontent_index >= remote_content_size) {
1539 LOG(LS_ERROR)
1540 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1541 return false;
1542 }
1543
1544 cricket::ContentInfo content =
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001545 base_remote_description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001546 std::vector<cricket::Candidate> candidates;
1547 candidates.push_back(candidate->candidate());
1548 // Invoking BaseSession method to handle remote candidates.
1549 std::string error;
1550 if (OnRemoteCandidates(content.name, candidates, &error)) {
1551 // Candidates successfully submitted for checking.
1552 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1553 ice_connection_state_ ==
1554 PeerConnectionInterface::kIceConnectionDisconnected) {
1555 // If state is New, then the session has just gotten its first remote ICE
1556 // candidates, so go to Checking.
1557 // If state is Disconnected, the session is re-using old candidates or
1558 // receiving additional ones, so go to Checking.
1559 // If state is Connected, stay Connected.
1560 // TODO(bemasc): If state is Connected, and the new candidates are for a
1561 // newly added transport, then the state actually _should_ move to
1562 // checking. Add a way to distinguish that case.
1563 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1564 }
1565 // TODO(bemasc): If state is Completed, go back to Connected.
1566 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001567 if (!error.empty()) {
1568 LOG(LS_WARNING) << error;
1569 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001570 }
1571 return true;
1572}
1573
1574void WebRtcSession::RemoveUnusedChannelsAndTransports(
1575 const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001576 // Destroy video_channel_ first since it may have a pointer to the
1577 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001578 const cricket::ContentInfo* video_info =
1579 cricket::GetFirstVideoContent(desc);
1580 if ((!video_info || video_info->rejected) && video_channel_) {
1581 mediastream_signaling_->OnVideoChannelClose();
1582 SignalVideoChannelDestroyed();
1583 const std::string content_name = video_channel_->content_name();
1584 channel_manager_->DestroyVideoChannel(video_channel_.release());
bjornv@webrtc.org520a69e2015-02-04 12:45:44 +00001585 DestroyTransportProxy(content_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586 }
1587
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001588 const cricket::ContentInfo* voice_info =
1589 cricket::GetFirstAudioContent(desc);
1590 if ((!voice_info || voice_info->rejected) && voice_channel_) {
1591 mediastream_signaling_->OnAudioChannelClose();
1592 SignalVoiceChannelDestroyed();
1593 const std::string content_name = voice_channel_->content_name();
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02001594 channel_manager_->DestroyVoiceChannel(voice_channel_.release(),
1595 video_channel_.get());
bjornv@webrtc.org520a69e2015-02-04 12:45:44 +00001596 DestroyTransportProxy(content_name);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001597 }
1598
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001599 const cricket::ContentInfo* data_info =
1600 cricket::GetFirstDataContent(desc);
1601 if ((!data_info || data_info->rejected) && data_channel_) {
1602 mediastream_signaling_->OnDataChannelClose();
1603 SignalDataChannelDestroyed();
1604 const std::string content_name = data_channel_->content_name();
1605 channel_manager_->DestroyDataChannel(data_channel_.release());
bjornv@webrtc.org520a69e2015-02-04 12:45:44 +00001606 DestroyTransportProxy(content_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001607 }
1608}
1609
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001610// TODO(mallinath) - Add a correct error code if the channels are not creatued
1611// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001612bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001613 // Creating the media channels and transport proxies.
1614 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1615 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001616 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001617 LOG(LS_ERROR) << "Failed to create voice channel.";
1618 return false;
1619 }
1620 }
1621
1622 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1623 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001624 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001625 LOG(LS_ERROR) << "Failed to create video channel.";
1626 return false;
1627 }
1628 }
1629
1630 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1631 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001632 data && !data->rejected && !data_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001633 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001634 LOG(LS_ERROR) << "Failed to create data channel.";
1635 return false;
1636 }
1637 }
1638
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001639 if (rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire) {
1640 if (voice_channel()) {
1641 voice_channel()->ActivateRtcpMux();
1642 }
1643 if (video_channel()) {
1644 video_channel()->ActivateRtcpMux();
1645 }
1646 if (data_channel()) {
1647 data_channel()->ActivateRtcpMux();
1648 }
1649 }
1650
Donald Curtis0e209b02015-03-24 09:29:54 -07001651 // Enable bundle before when kMaxBundle policy is in effect.
1652 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
Peter Thatcher4eddf182015-04-30 10:55:59 -07001653 const cricket::ContentGroup* bundle_group = desc->GetGroupByName(
1654 cricket::GROUP_TYPE_BUNDLE);
1655 if (!bundle_group) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001656 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1657 return false;
1658 }
Peter Thatcher4eddf182015-04-30 10:55:59 -07001659 if (!BaseSession::BundleContentGroup(bundle_group)) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001660 LOG(LS_WARNING) << "max-bundle failed to enable bundling.";
1661 return false;
1662 }
1663 }
1664
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665 return true;
1666}
1667
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001668bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
Jelena Marusicc28a8962015-05-29 15:05:44 +02001670 this, content->name, true, audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001671 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001672 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001673 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001674
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001675 voice_channel_->SignalDtlsSetupFailure.connect(
1676 this, &WebRtcSession::OnDtlsSetupFailure);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001677 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678}
1679
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001680bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001681 video_channel_.reset(channel_manager_->CreateVideoChannel(
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +00001682 this, content->name, true, video_options_, voice_channel_.get()));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001683 if (!video_channel_) {
1684 return false;
1685 }
1686
1687 video_channel_->SignalDtlsSetupFailure.connect(
1688 this, &WebRtcSession::OnDtlsSetupFailure);
1689 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001690}
1691
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001692bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001693 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001694 data_channel_.reset(channel_manager_->CreateDataChannel(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001695 this, content->name, !sctp, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001696 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001697 return false;
1698 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001699
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001700 if (sctp) {
1701 mediastream_signaling_->OnDataTransportCreatedForSctp();
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001702 data_channel_->SignalDataReceived.connect(
1703 this, &WebRtcSession::OnDataChannelMessageReceived);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00001704 data_channel_->SignalStreamClosedRemotely.connect(
1705 mediastream_signaling_,
1706 &MediaStreamSignaling::OnRemoteSctpDataChannelClosed);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001707 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001708
1709 data_channel_->SignalDtlsSetupFailure.connect(
1710 this, &WebRtcSession::OnDtlsSetupFailure);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001711 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001712}
1713
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001714void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
1715 SetError(BaseSession::ERROR_TRANSPORT, rtcp ? kDtlsSetupFailureRtcp :
1716 kDtlsSetupFailureRtp);
1717}
1718
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001719void WebRtcSession::CopySavedCandidates(
1720 SessionDescriptionInterface* dest_desc) {
1721 if (!dest_desc) {
1722 ASSERT(false);
1723 return;
1724 }
1725 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
1726 dest_desc->AddCandidate(saved_candidates_[i]);
1727 delete saved_candidates_[i];
1728 }
1729 saved_candidates_.clear();
1730}
1731
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001732void WebRtcSession::OnDataChannelMessageReceived(
1733 cricket::DataChannel* channel,
1734 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001735 const rtc::Buffer& payload) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001736 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001737 if (params.type == cricket::DMT_CONTROL &&
1738 mediastream_signaling_->IsSctpSidAvailable(params.ssrc)) {
1739 // Received CONTROL on unused sid, process as an OPEN message.
1740 mediastream_signaling_->AddDataChannelFromOpenMessage(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001741 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001742 // otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001743}
1744
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001745// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001746bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001747 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1748 if (!bundle_enabled)
1749 return true;
1750
1751 const cricket::ContentGroup* bundle_group =
1752 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1753 ASSERT(bundle_group != NULL);
1754
1755 const cricket::ContentInfos& contents = desc->contents();
1756 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1757 citer != contents.end(); ++citer) {
1758 const cricket::ContentInfo* content = (&*citer);
1759 ASSERT(content != NULL);
1760 if (bundle_group->HasContentName(content->name) &&
1761 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1762 if (!HasRtcpMuxEnabled(content))
1763 return false;
1764 }
1765 }
1766 // RTCP-MUX is enabled in all the contents.
1767 return true;
1768}
1769
1770bool WebRtcSession::HasRtcpMuxEnabled(
1771 const cricket::ContentInfo* content) {
1772 const cricket::MediaContentDescription* description =
1773 static_cast<cricket::MediaContentDescription*>(content->description);
1774 return description->rtcp_mux();
1775}
1776
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001777bool WebRtcSession::ValidateSessionDescription(
1778 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001779 cricket::ContentSource source, std::string* err_desc) {
1780 std::string type;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001781 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001782 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001783 }
1784
1785 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001786 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001787 }
1788
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001789 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001790 Action action = GetAction(sdesc->type());
1791 if (source == cricket::CS_LOCAL) {
1792 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001793 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001794 } else {
1795 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001796 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001797 }
1798
1799 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001800 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001801 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1802 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001803 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001804 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001805 }
1806
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001807 // Verify ice-ufrag and ice-pwd.
1808 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001809 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001810 }
1811
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001812 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001813 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001814 }
1815
1816 // Verify m-lines in Answer when compared against Offer.
1817 if (action == kAnswer) {
1818 const cricket::SessionDescription* offer_desc =
1819 (source == cricket::CS_LOCAL) ? remote_description()->description() :
1820 local_description()->description();
1821 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001822 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001823 }
1824 }
1825
1826 return true;
1827}
1828
1829bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1830 return ((action == kOffer && state() == STATE_INIT) ||
1831 // update local offer
1832 (action == kOffer && state() == STATE_SENTINITIATE) ||
1833 // update the current ongoing session.
1834 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1835 (action == kOffer && state() == STATE_SENTACCEPT) ||
1836 (action == kOffer && state() == STATE_INPROGRESS) ||
1837 // accept remote offer
1838 (action == kAnswer && state() == STATE_RECEIVEDINITIATE) ||
1839 (action == kAnswer && state() == STATE_SENTPRACCEPT) ||
1840 (action == kPrAnswer && state() == STATE_RECEIVEDINITIATE) ||
1841 (action == kPrAnswer && state() == STATE_SENTPRACCEPT));
1842}
1843
1844bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1845 return ((action == kOffer && state() == STATE_INIT) ||
1846 // update remote offer
1847 (action == kOffer && state() == STATE_RECEIVEDINITIATE) ||
1848 // update the current ongoing session
1849 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1850 (action == kOffer && state() == STATE_SENTACCEPT) ||
1851 (action == kOffer && state() == STATE_INPROGRESS) ||
1852 // accept local offer
1853 (action == kAnswer && state() == STATE_SENTINITIATE) ||
1854 (action == kAnswer && state() == STATE_RECEIVEDPRACCEPT) ||
1855 (action == kPrAnswer && state() == STATE_SENTINITIATE) ||
1856 (action == kPrAnswer && state() == STATE_RECEIVEDPRACCEPT));
1857}
1858
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001859std::string WebRtcSession::GetSessionErrorMsg() {
1860 std::ostringstream desc;
1861 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1862 desc << kSessionErrorDesc << error_desc() << ".";
1863 return desc.str();
1864}
1865
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001866// We need to check the local/remote description for the Transport instead of
1867// the session, because a new Transport added during renegotiation may have
1868// them unset while the session has them set from the previous negotiation.
1869// Not doing so may trigger the auto generation of transport description and
1870// mess up DTLS identity information, ICE credential, etc.
1871bool WebRtcSession::ReadyToUseRemoteCandidate(
1872 const IceCandidateInterface* candidate,
1873 const SessionDescriptionInterface* remote_desc,
1874 bool* valid) {
1875 *valid = true;;
1876 cricket::TransportProxy* transport_proxy = NULL;
1877
1878 const SessionDescriptionInterface* current_remote_desc =
1879 remote_desc ? remote_desc : remote_description();
1880
1881 if (!current_remote_desc)
1882 return false;
1883
1884 size_t mediacontent_index =
1885 static_cast<size_t>(candidate->sdp_mline_index());
1886 size_t remote_content_size =
1887 current_remote_desc->description()->contents().size();
1888 if (mediacontent_index >= remote_content_size) {
1889 LOG(LS_ERROR)
1890 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
1891
1892 *valid = false;
1893 return false;
1894 }
1895
1896 cricket::ContentInfo content =
1897 current_remote_desc->description()->contents()[mediacontent_index];
1898 transport_proxy = GetTransportProxy(content.name);
1899
1900 return transport_proxy && transport_proxy->local_description_set() &&
1901 transport_proxy->remote_description_set();
1902}
1903
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001904// Walk through the ConnectionInfos to gather best connection usage
1905// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07001906void WebRtcSession::ReportBestConnectionState(
1907 const cricket::TransportStats& stats) {
1908 DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001909 for (cricket::TransportChannelStatsList::const_iterator it =
1910 stats.channel_stats.begin();
1911 it != stats.channel_stats.end(); ++it) {
1912 for (cricket::ConnectionInfos::const_iterator it_info =
1913 it->connection_infos.begin();
1914 it_info != it->connection_infos.end(); ++it_info) {
1915 if (!it_info->best_connection) {
1916 continue;
1917 }
1918 if (it_info->local_candidate.address().family() == AF_INET) {
1919 metrics_observer_->IncrementCounter(kBestConnections_IPv4);
1920 } else if (it_info->local_candidate.address().family() ==
1921 AF_INET6) {
1922 metrics_observer_->IncrementCounter(kBestConnections_IPv6);
1923 } else {
jbauchac8869e2015-07-03 01:36:14 -07001924 RTC_NOTREACHED();
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001925 }
1926 return;
1927 }
1928 }
1929}
1930
jbauchac8869e2015-07-03 01:36:14 -07001931void WebRtcSession::ReportNegotiatedCiphers(
1932 const cricket::TransportStats& stats) {
1933 DCHECK(metrics_observer_ != NULL);
1934 if (!dtls_enabled_ || stats.channel_stats.empty()) {
1935 return;
1936 }
1937
1938 const std::string& srtp_cipher = stats.channel_stats[0].srtp_cipher;
1939 const std::string& ssl_cipher = stats.channel_stats[0].ssl_cipher;
1940 if (srtp_cipher.empty() && ssl_cipher.empty()) {
1941 return;
1942 }
1943
1944 PeerConnectionMetricsName srtp_name;
1945 PeerConnectionMetricsName ssl_name;
1946 if (stats.content_name == cricket::CN_AUDIO) {
1947 srtp_name = kAudioSrtpCipher;
1948 ssl_name = kAudioSslCipher;
1949 } else if (stats.content_name == cricket::CN_VIDEO) {
1950 srtp_name = kVideoSrtpCipher;
1951 ssl_name = kVideoSslCipher;
1952 } else if (stats.content_name == cricket::CN_DATA) {
1953 srtp_name = kDataSrtpCipher;
1954 ssl_name = kDataSslCipher;
1955 } else {
1956 RTC_NOTREACHED();
1957 return;
1958 }
1959
1960 if (!srtp_cipher.empty()) {
1961 metrics_observer_->AddHistogramSample(srtp_name, srtp_cipher);
1962 }
1963 if (!ssl_cipher.empty()) {
1964 metrics_observer_->AddHistogramSample(ssl_name, ssl_cipher);
1965 }
1966}
1967
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001968} // namespace webrtc