blob: 5c8b2df754fe8d69dd5928e2a0477e3f2e8e50ab [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"
47#include "webrtc/base/helpers.h"
48#include "webrtc/base/logging.h"
49#include "webrtc/base/stringencode.h"
50#include "webrtc/base/stringutils.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000051#include "webrtc/p2p/base/portallocator.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052
53using cricket::ContentInfo;
54using cricket::ContentInfos;
55using cricket::MediaContentDescription;
56using cricket::SessionDescription;
57using cricket::TransportInfo;
58
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059namespace webrtc {
60
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000062const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
63 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000064const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065const char kInvalidCandidates[] = "Description contains invalid candidates.";
66const char kInvalidSdp[] = "Invalid session description.";
67const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000068 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
69const char kPushDownTDFailed[] =
70 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000071const char kSdpWithoutDtlsFingerprint[] =
72 "Called with SDP without DTLS fingerprint.";
73const char kSdpWithoutSdesCrypto[] =
74 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000075const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000076 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000078const char kSessionErrorDesc[] = "Session error description: ";
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000079const char kDtlsSetupFailureRtp[] =
80 "Couldn't set up DTLS-SRTP on RTP channel.";
81const char kDtlsSetupFailureRtcp[] =
82 "Couldn't set up DTLS-SRTP on RTCP channel.";
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +000083const int kMaxUnsignalledRecvStreams = 20;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084
85// Compares |answer| against |offer|. Comparision is done
86// for number of m-lines in answer against offer. If matches true will be
87// returned otherwise false.
88static bool VerifyMediaDescriptions(
89 const SessionDescription* answer, const SessionDescription* offer) {
90 if (offer->contents().size() != answer->contents().size())
91 return false;
92
93 for (size_t i = 0; i < offer->contents().size(); ++i) {
94 if ((offer->contents()[i].name) != answer->contents()[i].name) {
95 return false;
96 }
wu@webrtc.org4e393072014-04-07 17:04:35 +000097 const MediaContentDescription* offer_mdesc =
98 static_cast<const MediaContentDescription*>(
99 offer->contents()[i].description);
100 const MediaContentDescription* answer_mdesc =
101 static_cast<const MediaContentDescription*>(
102 answer->contents()[i].description);
103 if (offer_mdesc->type() != answer_mdesc->type()) {
104 return false;
105 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106 }
107 return true;
108}
109
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110// Checks that each non-rejected content has SDES crypto keys or a DTLS
111// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
112// keys, will be caught in Transport negotiation, and backstopped by Channel's
113// |secure_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000114static bool VerifyCrypto(const SessionDescription* desc,
115 bool dtls_enabled,
116 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117 const ContentInfos& contents = desc->contents();
118 for (size_t index = 0; index < contents.size(); ++index) {
119 const ContentInfo* cinfo = &contents[index];
120 if (cinfo->rejected) {
121 continue;
122 }
123
124 // If the content isn't rejected, crypto must be present.
125 const MediaContentDescription* media =
126 static_cast<const MediaContentDescription*>(cinfo->description);
127 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
128 if (!media || !tinfo) {
129 // Something is not right.
130 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000131 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132 return false;
133 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000134 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000135 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000136 LOG(LS_WARNING) <<
137 "Session description must have DTLS fingerprint if DTLS enabled.";
138 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000139 return false;
140 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000141 } else {
142 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000143 LOG(LS_WARNING) <<
144 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000145 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000146 return false;
147 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 }
149 }
150
151 return true;
152}
153
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000154// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
155static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
156 const ContentInfos& contents = desc->contents();
157 for (size_t index = 0; index < contents.size(); ++index) {
158 const ContentInfo* cinfo = &contents[index];
159 if (cinfo->rejected) {
160 continue;
161 }
162
163 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
164 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
165 if (!tinfo) {
166 // Something is not right.
167 LOG(LS_ERROR) << kInvalidSdp;
168 return false;
169 }
170 if (tinfo->description.ice_ufrag.empty() ||
171 tinfo->description.ice_pwd.empty()) {
172 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
173 return false;
174 }
175 }
176 return true;
177}
178
wu@webrtc.org91053e72013-08-10 07:18:04 +0000179// Forces |sdesc->crypto_required| to the appropriate state based on the
180// current security policy, to ensure a failure occurs if there is an error
181// in crypto negotiation.
182// Called when processing the local session description.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000183static void UpdateSessionDescriptionSecurePolicy(cricket::CryptoType type,
184 SessionDescription* sdesc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000185 if (!sdesc) {
186 return;
187 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188
wu@webrtc.org91053e72013-08-10 07:18:04 +0000189 // Updating the |crypto_required_| in MediaContentDescription to the
190 // appropriate state based on the current security policy.
191 for (cricket::ContentInfos::iterator iter = sdesc->contents().begin();
192 iter != sdesc->contents().end(); ++iter) {
193 if (cricket::IsMediaContent(&*iter)) {
194 MediaContentDescription* mdesc =
195 static_cast<MediaContentDescription*> (iter->description);
196 if (mdesc) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000197 mdesc->set_crypto_required(type);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000198 }
199 }
200 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201}
202
203static bool GetAudioSsrcByTrackId(
204 const SessionDescription* session_description,
205 const std::string& track_id, uint32 *ssrc) {
206 const cricket::ContentInfo* audio_info =
207 cricket::GetFirstAudioContent(session_description);
208 if (!audio_info) {
209 LOG(LS_ERROR) << "Audio not used in this call";
210 return false;
211 }
212
213 const cricket::MediaContentDescription* audio_content =
214 static_cast<const cricket::MediaContentDescription*>(
215 audio_info->description);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000216 const cricket::StreamParams* stream =
217 cricket::GetStreamByIds(audio_content->streams(), "", track_id);
218 if (!stream) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219 return false;
220 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000221
222 *ssrc = stream->first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000223 return true;
224}
225
226static bool GetTrackIdBySsrc(const SessionDescription* session_description,
227 uint32 ssrc, std::string* track_id) {
228 ASSERT(track_id != NULL);
229
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230 const cricket::ContentInfo* audio_info =
231 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000232 if (audio_info) {
233 const cricket::MediaContentDescription* audio_content =
234 static_cast<const cricket::MediaContentDescription*>(
235 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000237 const auto* found =
238 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
239 if (found) {
240 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000241 return true;
242 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000243 }
244
245 const cricket::ContentInfo* video_info =
246 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000247 if (video_info) {
248 const cricket::MediaContentDescription* video_content =
249 static_cast<const cricket::MediaContentDescription*>(
250 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000252 const auto* found =
253 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
254 if (found) {
255 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000256 return true;
257 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258 }
259 return false;
260}
261
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000262static bool BadSdp(const std::string& source,
263 const std::string& type,
264 const std::string& reason,
265 std::string* err_desc) {
266 std::ostringstream desc;
267 desc << "Failed to set " << source << " " << type << " sdp: " << reason;
268
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000270 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000272 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273 return false;
274}
275
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000277 const std::string& type,
278 const std::string& reason,
279 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000281 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000283 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 }
285}
286
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000287static bool BadLocalSdp(const std::string& type,
288 const std::string& reason,
289 std::string* err_desc) {
290 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
291}
292
293static bool BadRemoteSdp(const std::string& type,
294 const std::string& reason,
295 std::string* err_desc) {
296 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
297}
298
299static bool BadOfferSdp(cricket::ContentSource source,
300 const std::string& reason,
301 std::string* err_desc) {
302 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
303}
304
305static bool BadPranswerSdp(cricket::ContentSource source,
306 const std::string& reason,
307 std::string* err_desc) {
308 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
309 reason, err_desc);
310}
311
312static bool BadAnswerSdp(cricket::ContentSource source,
313 const std::string& reason,
314 std::string* err_desc) {
315 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316}
317
318#define GET_STRING_OF_STATE(state) \
319 case cricket::BaseSession::state: \
320 result = #state; \
321 break;
322
323static std::string GetStateString(cricket::BaseSession::State state) {
324 std::string result;
325 switch (state) {
326 GET_STRING_OF_STATE(STATE_INIT)
327 GET_STRING_OF_STATE(STATE_SENTINITIATE)
328 GET_STRING_OF_STATE(STATE_RECEIVEDINITIATE)
329 GET_STRING_OF_STATE(STATE_SENTPRACCEPT)
330 GET_STRING_OF_STATE(STATE_SENTACCEPT)
331 GET_STRING_OF_STATE(STATE_RECEIVEDPRACCEPT)
332 GET_STRING_OF_STATE(STATE_RECEIVEDACCEPT)
333 GET_STRING_OF_STATE(STATE_SENTMODIFY)
334 GET_STRING_OF_STATE(STATE_RECEIVEDMODIFY)
335 GET_STRING_OF_STATE(STATE_SENTREJECT)
336 GET_STRING_OF_STATE(STATE_RECEIVEDREJECT)
337 GET_STRING_OF_STATE(STATE_SENTREDIRECT)
338 GET_STRING_OF_STATE(STATE_SENTTERMINATE)
339 GET_STRING_OF_STATE(STATE_RECEIVEDTERMINATE)
340 GET_STRING_OF_STATE(STATE_INPROGRESS)
341 GET_STRING_OF_STATE(STATE_DEINIT)
342 default:
343 ASSERT(false);
344 break;
345 }
346 return result;
347}
348
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000349#define GET_STRING_OF_ERROR_CODE(err) \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000350 case cricket::BaseSession::err: \
351 result = #err; \
352 break;
353
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000354static std::string GetErrorCodeString(cricket::BaseSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355 std::string result;
356 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000357 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
358 GET_STRING_OF_ERROR_CODE(ERROR_TIME)
359 GET_STRING_OF_ERROR_CODE(ERROR_RESPONSE)
360 GET_STRING_OF_ERROR_CODE(ERROR_NETWORK)
361 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
362 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000363 default:
364 ASSERT(false);
365 break;
366 }
367 return result;
368}
369
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000370static std::string MakeErrorString(const std::string& error,
371 const std::string& desc) {
372 std::ostringstream ret;
373 ret << error << " " << desc;
374 return ret.str();
375}
376
377static std::string MakeTdErrorString(const std::string& desc) {
378 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379}
380
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000381// Set |option| to the highest-priority value of |key| in the optional
382// constraints if the key is found and has a valid value.
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000383template<typename T>
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000384static void SetOptionFromOptionalConstraint(
385 const MediaConstraintsInterface* constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000386 const std::string& key, cricket::Settable<T>* option) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000387 if (!constraints) {
388 return;
389 }
390 std::string string_value;
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000391 T value;
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000392 if (constraints->GetOptional().FindFirst(key, &string_value)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000393 if (rtc::FromString(string_value, &value)) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000394 option->Set(value);
395 }
396 }
397}
398
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000399uint32 ConvertIceTransportTypeToCandidateFilter(
400 PeerConnectionInterface::IceTransportsType type) {
401 switch (type) {
402 case PeerConnectionInterface::kNone:
403 return cricket::CF_NONE;
404 case PeerConnectionInterface::kRelay:
405 return cricket::CF_RELAY;
406 case PeerConnectionInterface::kNoHost:
407 return (cricket::CF_ALL & ~cricket::CF_HOST);
408 case PeerConnectionInterface::kAll:
409 return cricket::CF_ALL;
410 default: ASSERT(false);
411 }
412 return cricket::CF_NONE;
413}
414
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000415// Help class used to remember if a a remote peer has requested ice restart by
416// by sending a description with new ice ufrag and password.
417class IceRestartAnswerLatch {
418 public:
419 IceRestartAnswerLatch() : ice_restart_(false) { }
420
wu@webrtc.org91053e72013-08-10 07:18:04 +0000421 // Returns true if CheckForRemoteIceRestart has been called with a new session
422 // description where ice password and ufrag has changed since last time
423 // Reset() was called.
424 bool Get() const {
425 return ice_restart_;
426 }
427
428 void Reset() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000429 if (ice_restart_) {
430 ice_restart_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000431 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000432 }
433
434 void CheckForRemoteIceRestart(
435 const SessionDescriptionInterface* old_desc,
436 const SessionDescriptionInterface* new_desc) {
437 if (!old_desc || new_desc->type() != SessionDescriptionInterface::kOffer) {
438 return;
439 }
440 const SessionDescription* new_sd = new_desc->description();
441 const SessionDescription* old_sd = old_desc->description();
442 const ContentInfos& contents = new_sd->contents();
443 for (size_t index = 0; index < contents.size(); ++index) {
444 const ContentInfo* cinfo = &contents[index];
445 if (cinfo->rejected) {
446 continue;
447 }
448 // If the content isn't rejected, check if ufrag and password has
449 // changed.
450 const cricket::TransportDescription* new_transport_desc =
451 new_sd->GetTransportDescriptionByName(cinfo->name);
452 const cricket::TransportDescription* old_transport_desc =
453 old_sd->GetTransportDescriptionByName(cinfo->name);
454 if (!new_transport_desc || !old_transport_desc) {
455 // No transport description exist. This is not an ice restart.
456 continue;
457 }
jiayl@webrtc.orgdb397e52014-06-20 16:32:09 +0000458 if (cricket::IceCredentialsChanged(old_transport_desc->ice_ufrag,
459 old_transport_desc->ice_pwd,
460 new_transport_desc->ice_ufrag,
461 new_transport_desc->ice_pwd)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462 LOG(LS_INFO) << "Remote peer request ice restart.";
463 ice_restart_ = true;
464 break;
465 }
466 }
467 }
468
469 private:
470 bool ice_restart_;
471};
472
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000473WebRtcSession::WebRtcSession(
474 cricket::ChannelManager* channel_manager,
475 rtc::Thread* signaling_thread,
476 rtc::Thread* worker_thread,
477 cricket::PortAllocator* port_allocator,
478 MediaStreamSignaling* mediastream_signaling)
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000479 : cricket::BaseSession(signaling_thread,
480 worker_thread,
481 port_allocator,
482 rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX),
483 cricket::NS_JINGLE_RTP,
484 false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 // RFC 3264: The numeric value of the session id and version in the
486 // o line MUST be representable with a "64 bit signed integer".
487 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
488 channel_manager_(channel_manager),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489 mediastream_signaling_(mediastream_signaling),
490 ice_observer_(NULL),
491 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000493 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000495 ice_restart_latch_(new IceRestartAnswerLatch),
496 metrics_observer_(NULL) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497}
498
499WebRtcSession::~WebRtcSession() {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000500 // Destroy video_channel_ first since it may have a pointer to the
501 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000502 if (video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503 SignalVideoChannelDestroyed();
504 channel_manager_->DestroyVideoChannel(video_channel_.release());
505 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000506 if (voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000507 SignalVoiceChannelDestroyed();
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200508 channel_manager_->DestroyVoiceChannel(voice_channel_.release(), nullptr);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000509 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000510 if (data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 SignalDataChannelDestroyed();
512 channel_manager_->DestroyDataChannel(data_channel_.release());
513 }
514 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
515 delete saved_candidates_[i];
516 }
517 delete identity();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000518}
519
wu@webrtc.org91053e72013-08-10 07:18:04 +0000520bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000521 const PeerConnectionFactoryInterface::Options& options,
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000522 const MediaConstraintsInterface* constraints,
523 DTLSIdentityServiceInterface* dtls_identity_service,
Henrik Lundin64dad832015-05-11 12:44:23 +0200524 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
525 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700526 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
Joachim Bauch04e5b492015-05-29 09:40:39 +0200527 SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000528
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529 // TODO(perkj): Take |constraints| into consideration. Return false if not all
530 // mandatory constraints can be fulfilled. Note that |constraints|
531 // can be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532 bool value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000533
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000534 if (options.disable_encryption) {
535 dtls_enabled_ = false;
536 } else {
537 // Enable DTLS by default if |dtls_identity_service| is valid.
538 dtls_enabled_ = (dtls_identity_service != NULL);
539 // |constraints| can override the default |dtls_enabled_| value.
540 if (FindConstraint(
541 constraints,
542 MediaConstraintsInterface::kEnableDtlsSrtp,
543 &value, NULL)) {
544 dtls_enabled_ = value;
545 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000546 }
547
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000549 // It takes precendence over the disable_sctp_data_channels
550 // PeerConnectionFactoryInterface::Options.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000551 if (FindConstraint(
552 constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
553 &value, NULL) && value) {
554 LOG(LS_INFO) << "Allowing RTP data engine.";
555 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000556 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000557 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000558 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000559 LOG(LS_INFO) << "Allowing SCTP data engine.";
560 data_channel_type_ = cricket::DCT_SCTP;
561 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000562 }
563 if (data_channel_type_ != cricket::DCT_NONE) {
564 mediastream_signaling_->SetDataChannelFactory(this);
565 }
566
wu@webrtc.orgde305012013-10-31 15:40:38 +0000567 // Find DSCP constraint.
568 if (FindConstraint(
569 constraints,
570 MediaConstraintsInterface::kEnableDscp,
571 &value, NULL)) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000572 audio_options_.dscp.Set(value);
573 video_options_.dscp.Set(value);
574 }
575
576 // Find Suspend Below Min Bitrate constraint.
577 if (FindConstraint(
578 constraints,
579 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
580 &value,
581 NULL)) {
582 video_options_.suspend_below_min_bitrate.Set(value);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000583 }
584
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000585 SetOptionFromOptionalConstraint(constraints,
586 MediaConstraintsInterface::kScreencastMinBitrate,
587 &video_options_.screencast_min_bitrate);
588
589 // Find constraints for cpu overuse detection.
590 SetOptionFromOptionalConstraint(constraints,
591 MediaConstraintsInterface::kCpuUnderuseThreshold,
592 &video_options_.cpu_underuse_threshold);
593 SetOptionFromOptionalConstraint(constraints,
594 MediaConstraintsInterface::kCpuOveruseThreshold,
595 &video_options_.cpu_overuse_threshold);
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000596 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000597 MediaConstraintsInterface::kCpuOveruseDetection,
598 &video_options_.cpu_overuse_detection);
599 SetOptionFromOptionalConstraint(constraints,
600 MediaConstraintsInterface::kCpuOveruseEncodeUsage,
601 &video_options_.cpu_overuse_encode_usage);
602 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000603 MediaConstraintsInterface::kCpuUnderuseEncodeRsdThreshold,
604 &video_options_.cpu_underuse_encode_rsd_threshold);
605 SetOptionFromOptionalConstraint(constraints,
606 MediaConstraintsInterface::kCpuOveruseEncodeRsdThreshold,
607 &video_options_.cpu_overuse_encode_rsd_threshold);
buildbot@webrtc.orgdb563902014-06-13 13:05:48 +0000608
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000609 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000610 MediaConstraintsInterface::kNumUnsignalledRecvStreams,
611 &video_options_.unsignalled_recv_stream_limit);
612 if (video_options_.unsignalled_recv_stream_limit.IsSet()) {
613 int stream_limit;
614 video_options_.unsignalled_recv_stream_limit.Get(&stream_limit);
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000615 stream_limit = std::min(kMaxUnsignalledRecvStreams, stream_limit);
616 stream_limit = std::max(0, stream_limit);
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000617 video_options_.unsignalled_recv_stream_limit.Set(stream_limit);
618 }
619
620 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000621 MediaConstraintsInterface::kHighStartBitrate,
622 &video_options_.video_start_bitrate);
wu@webrtc.orgcfe5e9c2014-03-27 17:03:58 +0000623
624 if (FindConstraint(
625 constraints,
626 MediaConstraintsInterface::kVeryHighBitrate,
627 &value,
628 NULL)) {
629 video_options_.video_highest_bitrate.Set(
630 cricket::VideoOptions::VERY_HIGH);
631 } else if (FindConstraint(
632 constraints,
633 MediaConstraintsInterface::kHighBitrate,
634 &value,
635 NULL)) {
636 video_options_.video_highest_bitrate.Set(
637 cricket::VideoOptions::HIGH);
638 }
639
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000640 SetOptionFromOptionalConstraint(constraints,
641 MediaConstraintsInterface::kCombinedAudioVideoBwe,
642 &audio_options_.combined_audio_video_bwe);
643
Henrik Lundin64dad832015-05-11 12:44:23 +0200644 audio_options_.audio_jitter_buffer_max_packets.Set(
645 rtc_configuration.audio_jitter_buffer_max_packets);
646
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200647 audio_options_.audio_jitter_buffer_fast_accelerate.Set(
648 rtc_configuration.audio_jitter_buffer_fast_accelerate);
649
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000650 const cricket::VideoCodec default_codec(
651 JsepSessionDescription::kDefaultVideoCodecId,
652 JsepSessionDescription::kDefaultVideoCodecName,
653 JsepSessionDescription::kMaxVideoCodecWidth,
654 JsepSessionDescription::kMaxVideoCodecHeight,
655 JsepSessionDescription::kDefaultVideoCodecFramerate,
656 JsepSessionDescription::kDefaultVideoCodecPreference);
657 channel_manager_->SetDefaultVideoEncoderConfig(
658 cricket::VideoEncoderConfig(default_codec));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000659
660 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
661 signaling_thread(),
662 channel_manager_,
663 mediastream_signaling_,
664 dtls_identity_service,
665 this,
666 id(),
667 data_channel_type_,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000668 dtls_enabled_));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000669
670 webrtc_session_desc_factory_->SignalIdentityReady.connect(
671 this, &WebRtcSession::OnIdentityReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000672
wu@webrtc.org97077a32013-10-25 21:18:33 +0000673 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000674 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000675 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000676 port_allocator()->set_candidate_filter(
Henrik Lundin64dad832015-05-11 12:44:23 +0200677 ConvertIceTransportTypeToCandidateFilter(rtc_configuration.type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000678 return true;
679}
680
681void WebRtcSession::Terminate() {
682 SetState(STATE_RECEIVEDTERMINATE);
683 RemoveUnusedChannelsAndTransports(NULL);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000684 ASSERT(!voice_channel_);
685 ASSERT(!video_channel_);
686 ASSERT(!data_channel_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687}
688
689bool WebRtcSession::StartCandidatesAllocation() {
690 // SpeculativelyConnectTransportChannels, will call ConnectChannels method
691 // from TransportProxy to start gathering ice candidates.
692 SpeculativelyConnectAllTransportChannels();
693 if (!saved_candidates_.empty()) {
694 // If there are saved candidates which arrived before local description is
695 // set, copy those to remote description.
696 CopySavedCandidates(remote_desc_.get());
697 }
698 // Push remote candidates present in remote description to transport channels.
699 UseCandidatesInSessionDescription(remote_desc_.get());
700 return true;
701}
702
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000703void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
704 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705}
706
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000707cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
708 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709}
710
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000711bool WebRtcSession::GetSslRole(rtc::SSLRole* role) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000712 if (local_description() == NULL || remote_description() == NULL) {
713 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
714 << "SSL Role of the session.";
715 return false;
716 }
717
718 // TODO(mallinath) - Return role of each transport, as role may differ from
719 // one another.
720 // In current implementaion we just return the role of first transport in the
721 // transport map.
722 for (cricket::TransportMap::const_iterator iter = transport_proxies().begin();
723 iter != transport_proxies().end(); ++iter) {
724 if (iter->second->impl()) {
725 return iter->second->impl()->GetSslRole(role);
726 }
727 }
728 return false;
729}
730
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000731void WebRtcSession::CreateOffer(
732 CreateSessionDescriptionObserver* observer,
733 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
734 webrtc_session_desc_factory_->CreateOffer(observer, options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000735}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000736
wu@webrtc.org91053e72013-08-10 07:18:04 +0000737void WebRtcSession::CreateAnswer(CreateSessionDescriptionObserver* observer,
738 const MediaConstraintsInterface* constraints) {
739 webrtc_session_desc_factory_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000740}
741
742bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
743 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000744 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000745 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000746
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000747 // Validate SDP.
748 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
749 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750 }
751
752 // Update the initiator flag if this session is the initiator.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000753 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000754 if (state() == STATE_INIT && action == kOffer) {
755 set_initiator(true);
756 }
757
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000758 cricket::SecurePolicy sdes_policy =
759 webrtc_session_desc_factory_->SdesPolicy();
760 cricket::CryptoType crypto_required = dtls_enabled_ ?
761 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
762 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000764 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765
766 set_local_description(desc->description()->Copy());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000767 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768
769 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000770 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771 // TODO(mallinath) - Handle CreateChannel failure, as new local description
772 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000773 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000774 }
775
776 // Remove channel and transport proxies, if MediaContentDescription is
777 // rejected.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000778 RemoveUnusedChannelsAndTransports(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000779
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000780 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 return false;
782 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000783
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 // Kick starting the ice candidates allocation.
785 StartCandidatesAllocation();
786
787 // Update state and SSRC of local MediaStreams and DataChannels based on the
788 // local session description.
789 mediastream_signaling_->OnLocalDescriptionChanged(local_desc_.get());
790
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000791 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000792 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
793 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
794 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000796 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797 }
798 return true;
799}
800
801bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
802 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000803 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000804 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000805
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000806 // Validate SDP.
807 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
808 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000809 }
810
811 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000812 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 if (action == kOffer && !CreateChannels(desc->description())) {
814 // TODO(mallinath) - Handle CreateChannel failure, as new local description
815 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000816 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 }
818
819 // Remove channel and transport proxies, if MediaContentDescription is
820 // rejected.
821 RemoveUnusedChannelsAndTransports(desc->description());
822
823 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
824 // is called.
825 set_remote_description(desc->description()->Copy());
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000826 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 return false;
828 }
829
830 // Update remote MediaStreams.
831 mediastream_signaling_->OnRemoteDescriptionChanged(desc);
832 if (local_description() && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000833 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000834 }
835
836 // Copy all saved candidates.
837 CopySavedCandidates(desc);
838 // We retain all received candidates.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000839 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
840 remote_desc_.get(), desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841 // Check if this new SessionDescription contains new ice ufrag and password
842 // that indicates the remote peer requests ice restart.
843 ice_restart_latch_->CheckForRemoteIceRestart(remote_desc_.get(),
844 desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000845 remote_desc_.reset(desc_temp.release());
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000846
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000847 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000848 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
849 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
850 }
851
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000853 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000855
856 // Set the the ICE connection state to connecting since the connection may
857 // become writable with peer reflexive candidates before any remote candidate
858 // is signaled.
859 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
860 // is to have a new signal the indicates a change in checking state from the
861 // transport and expose a new checking() member from transport that can be
862 // read to determine the current checking state. The existing SignalConnecting
863 // actually means "gathering candidates", so cannot be be used here.
864 if (desc->type() != SessionDescriptionInterface::kOffer &&
865 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
866 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
867 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868 return true;
869}
870
871bool WebRtcSession::UpdateSessionState(
872 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873 std::string* err_desc) {
874 // If there's already a pending error then no state transition should happen.
875 // But all call-sites should be verifying this before calling us!
876 ASSERT(error() == cricket::BaseSession::ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000877 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000879 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
880 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000881 }
882 SetState(source == cricket::CS_LOCAL ?
883 STATE_SENTINITIATE : STATE_RECEIVEDINITIATE);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000884 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
885 SetError(BaseSession::ERROR_CONTENT, *err_desc);
886 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000888 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889 }
890 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000891 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
892 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893 }
894 EnableChannels();
895 SetState(source == cricket::CS_LOCAL ?
896 STATE_SENTPRACCEPT : STATE_RECEIVEDPRACCEPT);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000897 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
898 SetError(BaseSession::ERROR_CONTENT, *err_desc);
899 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000901 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902 }
903 } else if (action == kAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000904 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
905 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906 }
907 MaybeEnableMuxingSupport();
908 EnableChannels();
909 SetState(source == cricket::CS_LOCAL ?
910 STATE_SENTACCEPT : STATE_RECEIVEDACCEPT);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000911 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
912 SetError(BaseSession::ERROR_CONTENT, *err_desc);
913 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000915 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916 }
917 }
918 return true;
919}
920
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000921bool WebRtcSession::PushdownMediaDescription(
922 cricket::ContentAction action,
923 cricket::ContentSource source,
924 std::string* err) {
925 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
926 if (!ch) {
927 return true;
928 } else if (source == cricket::CS_LOCAL) {
929 return ch->PushdownLocalDescription(
930 base_local_description(), action, err);
931 } else {
932 return ch->PushdownRemoteDescription(
933 base_remote_description(), action, err);
934 }
935 };
936
937 return (set_content(voice_channel()) &&
938 set_content(video_channel()) &&
939 set_content(data_channel()));
940}
941
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
943 if (type == SessionDescriptionInterface::kOffer) {
944 return WebRtcSession::kOffer;
945 } else if (type == SessionDescriptionInterface::kPrAnswer) {
946 return WebRtcSession::kPrAnswer;
947 } else if (type == SessionDescriptionInterface::kAnswer) {
948 return WebRtcSession::kAnswer;
949 }
950 ASSERT(false && "unknown action type");
951 return WebRtcSession::kOffer;
952}
953
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000954bool WebRtcSession::GetTransportStats(cricket::SessionStats* stats) {
955 ASSERT(signaling_thread()->IsCurrent());
956
957 const auto get_transport_stats = [stats](const std::string& content_name,
958 cricket::Transport* transport) {
959 const std::string& transport_id = transport->content_name();
960 stats->proxy_to_transport[content_name] = transport_id;
961 if (stats->transport_stats.find(transport_id)
962 != stats->transport_stats.end()) {
963 // Transport stats already done for this transport.
964 return true;
965 }
966
967 cricket::TransportStats tstats;
968 if (!transport->GetStats(&tstats)) {
969 return false;
970 }
971
972 stats->transport_stats[transport_id] = tstats;
973 return true;
974 };
975
976 for (const auto& kv : transport_proxies()) {
977 cricket::Transport* transport = kv.second->impl();
978 if (transport && !get_transport_stats(kv.first, transport)) {
979 return false;
980 }
981 }
982 return true;
983}
984
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000985bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
986 if (state() == STATE_INIT) {
987 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
988 << "without any offer (local or remote) "
989 << "session description.";
990 return false;
991 }
992
993 if (!candidate) {
994 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL";
995 return false;
996 }
997
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +0000998 bool valid = false;
999 if (!ReadyToUseRemoteCandidate(candidate, NULL, &valid)) {
1000 if (valid) {
1001 LOG(LS_INFO) << "ProcessIceMessage: Candidate saved";
1002 saved_candidates_.push_back(
1003 new JsepIceCandidate(candidate->sdp_mid(),
1004 candidate->sdp_mline_index(),
1005 candidate->candidate()));
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001006 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001007 return valid;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001008 }
1009
1010 // Add this candidate to the remote session description.
1011 if (!remote_desc_->AddCandidate(candidate)) {
1012 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used";
1013 return false;
1014 }
1015
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001016 return UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001017}
1018
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001019bool WebRtcSession::SetIceTransports(
1020 PeerConnectionInterface::IceTransportsType type) {
1021 return port_allocator()->set_candidate_filter(
1022 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001023}
1024
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001025bool WebRtcSession::GetLocalTrackIdBySsrc(uint32 ssrc, std::string* track_id) {
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001026 if (!base_local_description())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001027 return false;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001028 return webrtc::GetTrackIdBySsrc(base_local_description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001029}
1030
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001031bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32 ssrc, std::string* track_id) {
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001032 if (!base_remote_description())
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001033 return false;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001034 return webrtc::GetTrackIdBySsrc(base_remote_description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001035}
1036
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001037std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001038 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001039 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001040 return desc.str();
1041}
1042
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001043void WebRtcSession::SetAudioPlayout(uint32 ssrc, bool enable,
1044 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001045 ASSERT(signaling_thread()->IsCurrent());
1046 if (!voice_channel_) {
1047 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
1048 return;
1049 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001050 if (!voice_channel_->SetRemoteRenderer(ssrc, renderer)) {
1051 // SetRenderer() can fail if the ssrc does not match any playout channel.
1052 LOG(LS_ERROR) << "SetAudioPlayout: ssrc is incorrect: " << ssrc;
1053 return;
1054 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001055 if (!voice_channel_->SetOutputScaling(ssrc, enable ? 1 : 0, enable ? 1 : 0)) {
1056 // Allow that SetOutputScaling fail if |enable| is false but assert
1057 // otherwise. This in the normal case when the underlying media channel has
1058 // already been deleted.
1059 ASSERT(enable == false);
1060 }
1061}
1062
1063void WebRtcSession::SetAudioSend(uint32 ssrc, bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001064 const cricket::AudioOptions& options,
1065 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001066 ASSERT(signaling_thread()->IsCurrent());
1067 if (!voice_channel_) {
1068 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
1069 return;
1070 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001071 if (!voice_channel_->SetLocalRenderer(ssrc, renderer)) {
1072 // SetRenderer() can fail if the ssrc does not match any send channel.
1073 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
1074 return;
1075 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001076 if (!voice_channel_->MuteStream(ssrc, !enable)) {
1077 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1078 // This in the normal case when the underlying media channel has already
1079 // been deleted.
1080 ASSERT(enable == false);
1081 return;
1082 }
1083 if (enable)
1084 voice_channel_->SetChannelOptions(options);
1085}
1086
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001087void WebRtcSession::SetAudioPlayoutVolume(uint32 ssrc, double volume) {
1088 ASSERT(signaling_thread()->IsCurrent());
1089 ASSERT(volume >= 0 && volume <= 10);
1090 if (!voice_channel_) {
1091 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1092 return;
1093 }
1094
1095 if (!voice_channel_->SetOutputScaling(ssrc, volume, volume))
1096 ASSERT(false);
1097}
1098
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001099bool WebRtcSession::SetCaptureDevice(uint32 ssrc,
1100 cricket::VideoCapturer* camera) {
1101 ASSERT(signaling_thread()->IsCurrent());
1102
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001103 if (!video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001104 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1105 // support video.
1106 LOG(LS_WARNING) << "Video not used in this call.";
1107 return false;
1108 }
1109 if (!video_channel_->SetCapturer(ssrc, camera)) {
1110 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1111 // This in the normal case when the underlying media channel has already
1112 // been deleted.
1113 ASSERT(camera == NULL);
1114 return false;
1115 }
1116 return true;
1117}
1118
1119void WebRtcSession::SetVideoPlayout(uint32 ssrc,
1120 bool enable,
1121 cricket::VideoRenderer* renderer) {
1122 ASSERT(signaling_thread()->IsCurrent());
1123 if (!video_channel_) {
1124 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1125 return;
1126 }
1127 if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) {
1128 // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise.
1129 // This in the normal case when the underlying media channel has already
1130 // been deleted.
1131 ASSERT(renderer == NULL);
1132 }
1133}
1134
1135void WebRtcSession::SetVideoSend(uint32 ssrc, bool enable,
1136 const cricket::VideoOptions* options) {
1137 ASSERT(signaling_thread()->IsCurrent());
1138 if (!video_channel_) {
1139 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1140 return;
1141 }
1142 if (!video_channel_->MuteStream(ssrc, !enable)) {
1143 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1144 // This in the normal case when the underlying media channel has already
1145 // been deleted.
1146 ASSERT(enable == false);
1147 return;
1148 }
1149 if (enable && options)
1150 video_channel_->SetChannelOptions(*options);
1151}
1152
1153bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1154 ASSERT(signaling_thread()->IsCurrent());
1155 if (!voice_channel_) {
1156 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1157 return false;
1158 }
1159 uint32 send_ssrc = 0;
1160 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1161 // exists.
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001162 if (!GetAudioSsrcByTrackId(base_local_description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001163 &send_ssrc)) {
1164 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1165 return false;
1166 }
1167 return voice_channel_->CanInsertDtmf();
1168}
1169
1170bool WebRtcSession::InsertDtmf(const std::string& track_id,
1171 int code, int duration) {
1172 ASSERT(signaling_thread()->IsCurrent());
1173 if (!voice_channel_) {
1174 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1175 return false;
1176 }
1177 uint32 send_ssrc = 0;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001178 if (!VERIFY(GetAudioSsrcByTrackId(base_local_description(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001179 track_id, &send_ssrc))) {
1180 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1181 return false;
1182 }
1183 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration,
1184 cricket::DF_SEND)) {
1185 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1186 return false;
1187 }
1188 return true;
1189}
1190
1191sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
1192 return &SignalVoiceChannelDestroyed;
1193}
1194
wu@webrtc.org78187522013-10-07 23:32:02 +00001195bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001196 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001197 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001198 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001199 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1200 return false;
1201 }
1202 return data_channel_->SendData(params, payload, result);
1203}
1204
1205bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001206 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001207 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1208 return false;
1209 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001210 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1211 &DataChannel::OnChannelReady);
1212 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1213 &DataChannel::OnDataReceived);
wu@webrtc.org78187522013-10-07 23:32:02 +00001214 return true;
1215}
1216
1217void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001218 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001219 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1220 return;
1221 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001222 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1223 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
1224}
1225
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001226void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001227 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001228 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1229 return;
1230 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001231 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1232 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001233}
1234
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001235void WebRtcSession::RemoveSctpDataStream(int sid) {
1236 mediastream_signaling_->RemoveSctpDataChannel(sid);
jiayl@webrtc.org2eaac182014-06-17 16:02:46 +00001237
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001238 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001239 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1240 << "NULL.";
1241 return;
1242 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001243 data_channel_->RemoveRecvStream(sid);
1244 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001245}
1246
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001247bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001248 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001249}
1250
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001251rtc::scoped_refptr<DataChannel> WebRtcSession::CreateDataChannel(
wu@webrtc.org78187522013-10-07 23:32:02 +00001252 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001253 const InternalDataChannelInit* config) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254 if (state() == STATE_RECEIVEDTERMINATE) {
1255 return NULL;
1256 }
1257 if (data_channel_type_ == cricket::DCT_NONE) {
1258 LOG(LS_ERROR) << "CreateDataChannel: Data is not supported in this call.";
1259 return NULL;
1260 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001261 InternalDataChannelInit new_config =
1262 config ? (*config) : InternalDataChannelInit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001263 if (data_channel_type_ == cricket::DCT_SCTP) {
1264 if (new_config.id < 0) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001265 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001266 if (GetSslRole(&role) &&
1267 !mediastream_signaling_->AllocateSctpSid(role, &new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001268 LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel.";
1269 return NULL;
1270 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001271 } else if (!mediastream_signaling_->IsSctpSidAvailable(new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001272 LOG(LS_ERROR) << "Failed to create a SCTP data channel "
1273 << "because the id is already in use or out of range.";
1274 return NULL;
1275 }
1276 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001277
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001278 rtc::scoped_refptr<DataChannel> channel(DataChannel::Create(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001279 this, data_channel_type_, label, new_config));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001280 if (channel && !mediastream_signaling_->AddDataChannel(channel))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001281 return NULL;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001282
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001283 return channel;
1284}
1285
1286cricket::DataChannelType WebRtcSession::data_channel_type() const {
1287 return data_channel_type_;
1288}
1289
wu@webrtc.org91053e72013-08-10 07:18:04 +00001290bool WebRtcSession::IceRestartPending() const {
1291 return ice_restart_latch_->Get();
1292}
1293
1294void WebRtcSession::ResetIceRestartLatch() {
1295 ice_restart_latch_->Reset();
1296}
1297
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001298void WebRtcSession::OnIdentityReady(rtc::SSLIdentity* identity) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001299 SetIdentity(identity);
1300}
1301
1302bool WebRtcSession::waiting_for_identity() const {
1303 return webrtc_session_desc_factory_->waiting_for_identity();
1304}
1305
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001306void WebRtcSession::SetIceConnectionState(
1307 PeerConnectionInterface::IceConnectionState state) {
1308 if (ice_connection_state_ == state) {
1309 return;
1310 }
1311
1312 // ASSERT that the requested transition is allowed. Note that
1313 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1314 // within PeerConnection). This switch statement should compile away when
1315 // ASSERTs are disabled.
1316 switch (ice_connection_state_) {
1317 case PeerConnectionInterface::kIceConnectionNew:
1318 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1319 break;
1320 case PeerConnectionInterface::kIceConnectionChecking:
1321 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1322 state == PeerConnectionInterface::kIceConnectionConnected);
1323 break;
1324 case PeerConnectionInterface::kIceConnectionConnected:
1325 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1326 state == PeerConnectionInterface::kIceConnectionChecking ||
1327 state == PeerConnectionInterface::kIceConnectionCompleted);
1328 break;
1329 case PeerConnectionInterface::kIceConnectionCompleted:
1330 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1331 state == PeerConnectionInterface::kIceConnectionDisconnected);
1332 break;
1333 case PeerConnectionInterface::kIceConnectionFailed:
1334 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1335 break;
1336 case PeerConnectionInterface::kIceConnectionDisconnected:
1337 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1338 state == PeerConnectionInterface::kIceConnectionConnected ||
1339 state == PeerConnectionInterface::kIceConnectionCompleted ||
1340 state == PeerConnectionInterface::kIceConnectionFailed);
1341 break;
1342 case PeerConnectionInterface::kIceConnectionClosed:
1343 ASSERT(false);
1344 break;
1345 default:
1346 ASSERT(false);
1347 break;
1348 }
1349
1350 ice_connection_state_ = state;
1351 if (ice_observer_) {
1352 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1353 }
1354}
1355
1356void WebRtcSession::OnTransportRequestSignaling(
1357 cricket::Transport* transport) {
1358 ASSERT(signaling_thread()->IsCurrent());
1359 transport->OnSignalingReady();
1360 if (ice_observer_) {
1361 ice_observer_->OnIceGatheringChange(
1362 PeerConnectionInterface::kIceGatheringGathering);
1363 }
1364}
1365
1366void WebRtcSession::OnTransportConnecting(cricket::Transport* transport) {
1367 ASSERT(signaling_thread()->IsCurrent());
1368 // start monitoring for the write state of the transport.
1369 OnTransportWritable(transport);
1370}
1371
1372void WebRtcSession::OnTransportWritable(cricket::Transport* transport) {
1373 ASSERT(signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001374 if (transport->all_channels_writable()) {
henrike@webrtc.org05376342014-03-10 15:53:12 +00001375 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001376 } else if (transport->HasChannels()) {
1377 // If the current state is Connected or Completed, then there were writable
1378 // channels but now there are not, so the next state must be Disconnected.
1379 if (ice_connection_state_ ==
1380 PeerConnectionInterface::kIceConnectionConnected ||
1381 ice_connection_state_ ==
1382 PeerConnectionInterface::kIceConnectionCompleted) {
1383 SetIceConnectionState(
1384 PeerConnectionInterface::kIceConnectionDisconnected);
1385 }
1386 }
1387}
1388
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001389void WebRtcSession::OnTransportCompleted(cricket::Transport* transport) {
1390 ASSERT(signaling_thread()->IsCurrent());
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001391 PeerConnectionInterface::IceConnectionState old_state = ice_connection_state_;
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001392 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001393 // Only report once when Ice connection is completed.
1394 if (old_state != PeerConnectionInterface::kIceConnectionCompleted) {
1395 ReportBestConnectionState(transport);
1396 }
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001397}
1398
1399void WebRtcSession::OnTransportFailed(cricket::Transport* transport) {
1400 ASSERT(signaling_thread()->IsCurrent());
1401 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1402}
1403
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001404void WebRtcSession::OnTransportProxyCandidatesReady(
1405 cricket::TransportProxy* proxy, const cricket::Candidates& candidates) {
1406 ASSERT(signaling_thread()->IsCurrent());
1407 ProcessNewLocalCandidate(proxy->content_name(), candidates);
1408}
1409
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001410void WebRtcSession::OnCandidatesAllocationDone() {
1411 ASSERT(signaling_thread()->IsCurrent());
1412 if (ice_observer_) {
1413 ice_observer_->OnIceGatheringChange(
1414 PeerConnectionInterface::kIceGatheringComplete);
1415 ice_observer_->OnIceComplete();
1416 }
1417}
1418
1419// Enabling voice and video channel.
1420void WebRtcSession::EnableChannels() {
1421 if (voice_channel_ && !voice_channel_->enabled())
1422 voice_channel_->Enable(true);
1423
1424 if (video_channel_ && !video_channel_->enabled())
1425 video_channel_->Enable(true);
1426
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001427 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001428 data_channel_->Enable(true);
1429}
1430
1431void WebRtcSession::ProcessNewLocalCandidate(
1432 const std::string& content_name,
1433 const cricket::Candidates& candidates) {
1434 int sdp_mline_index;
1435 if (!GetLocalCandidateMediaIndex(content_name, &sdp_mline_index)) {
1436 LOG(LS_ERROR) << "ProcessNewLocalCandidate: content name "
1437 << content_name << " not found";
1438 return;
1439 }
1440
1441 for (cricket::Candidates::const_iterator citer = candidates.begin();
1442 citer != candidates.end(); ++citer) {
1443 // Use content_name as the candidate media id.
1444 JsepIceCandidate candidate(content_name, sdp_mline_index, *citer);
1445 if (ice_observer_) {
1446 ice_observer_->OnIceCandidate(&candidate);
1447 }
1448 if (local_desc_) {
1449 local_desc_->AddCandidate(&candidate);
1450 }
1451 }
1452}
1453
1454// Returns the media index for a local ice candidate given the content name.
1455bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1456 int* sdp_mline_index) {
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001457 if (!base_local_description() || !sdp_mline_index)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001458 return false;
1459
1460 bool content_found = false;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001461 const ContentInfos& contents = base_local_description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001462 for (size_t index = 0; index < contents.size(); ++index) {
1463 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001464 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001465 content_found = true;
1466 break;
1467 }
1468 }
1469 return content_found;
1470}
1471
1472bool WebRtcSession::UseCandidatesInSessionDescription(
1473 const SessionDescriptionInterface* remote_desc) {
1474 if (!remote_desc)
1475 return true;
1476 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001477
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001478 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1479 const IceCandidateCollection* candidates = remote_desc->candidates(m);
1480 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001481 const IceCandidateInterface* candidate = candidates->at(n);
1482 bool valid = false;
1483 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1484 if (valid) {
1485 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Candidate saved.";
1486 saved_candidates_.push_back(
1487 new JsepIceCandidate(candidate->sdp_mid(),
1488 candidate->sdp_mline_index(),
1489 candidate->candidate()));
1490 }
1491 continue;
1492 }
1493
1494 ret = UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001495 if (!ret)
1496 break;
1497 }
1498 }
1499 return ret;
1500}
1501
1502bool WebRtcSession::UseCandidate(
1503 const IceCandidateInterface* candidate) {
1504
1505 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001506 size_t remote_content_size = base_remote_description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001507 if (mediacontent_index >= remote_content_size) {
1508 LOG(LS_ERROR)
1509 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1510 return false;
1511 }
1512
1513 cricket::ContentInfo content =
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001514 base_remote_description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001515 std::vector<cricket::Candidate> candidates;
1516 candidates.push_back(candidate->candidate());
1517 // Invoking BaseSession method to handle remote candidates.
1518 std::string error;
1519 if (OnRemoteCandidates(content.name, candidates, &error)) {
1520 // Candidates successfully submitted for checking.
1521 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1522 ice_connection_state_ ==
1523 PeerConnectionInterface::kIceConnectionDisconnected) {
1524 // If state is New, then the session has just gotten its first remote ICE
1525 // candidates, so go to Checking.
1526 // If state is Disconnected, the session is re-using old candidates or
1527 // receiving additional ones, so go to Checking.
1528 // If state is Connected, stay Connected.
1529 // TODO(bemasc): If state is Connected, and the new candidates are for a
1530 // newly added transport, then the state actually _should_ move to
1531 // checking. Add a way to distinguish that case.
1532 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1533 }
1534 // TODO(bemasc): If state is Completed, go back to Connected.
1535 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001536 if (!error.empty()) {
1537 LOG(LS_WARNING) << error;
1538 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001539 }
1540 return true;
1541}
1542
1543void WebRtcSession::RemoveUnusedChannelsAndTransports(
1544 const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001545 // Destroy video_channel_ first since it may have a pointer to the
1546 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001547 const cricket::ContentInfo* video_info =
1548 cricket::GetFirstVideoContent(desc);
1549 if ((!video_info || video_info->rejected) && video_channel_) {
1550 mediastream_signaling_->OnVideoChannelClose();
1551 SignalVideoChannelDestroyed();
1552 const std::string content_name = video_channel_->content_name();
1553 channel_manager_->DestroyVideoChannel(video_channel_.release());
bjornv@webrtc.org520a69e2015-02-04 12:45:44 +00001554 DestroyTransportProxy(content_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001555 }
1556
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001557 const cricket::ContentInfo* voice_info =
1558 cricket::GetFirstAudioContent(desc);
1559 if ((!voice_info || voice_info->rejected) && voice_channel_) {
1560 mediastream_signaling_->OnAudioChannelClose();
1561 SignalVoiceChannelDestroyed();
1562 const std::string content_name = voice_channel_->content_name();
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02001563 channel_manager_->DestroyVoiceChannel(voice_channel_.release(),
1564 video_channel_.get());
bjornv@webrtc.org520a69e2015-02-04 12:45:44 +00001565 DestroyTransportProxy(content_name);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001566 }
1567
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001568 const cricket::ContentInfo* data_info =
1569 cricket::GetFirstDataContent(desc);
1570 if ((!data_info || data_info->rejected) && data_channel_) {
1571 mediastream_signaling_->OnDataChannelClose();
1572 SignalDataChannelDestroyed();
1573 const std::string content_name = data_channel_->content_name();
1574 channel_manager_->DestroyDataChannel(data_channel_.release());
bjornv@webrtc.org520a69e2015-02-04 12:45:44 +00001575 DestroyTransportProxy(content_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001576 }
1577}
1578
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001579// TODO(mallinath) - Add a correct error code if the channels are not creatued
1580// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001581bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001582 // Creating the media channels and transport proxies.
1583 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1584 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001585 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586 LOG(LS_ERROR) << "Failed to create voice channel.";
1587 return false;
1588 }
1589 }
1590
1591 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1592 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001593 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594 LOG(LS_ERROR) << "Failed to create video channel.";
1595 return false;
1596 }
1597 }
1598
1599 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1600 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001601 data && !data->rejected && !data_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001602 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001603 LOG(LS_ERROR) << "Failed to create data channel.";
1604 return false;
1605 }
1606 }
1607
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001608 if (rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire) {
1609 if (voice_channel()) {
1610 voice_channel()->ActivateRtcpMux();
1611 }
1612 if (video_channel()) {
1613 video_channel()->ActivateRtcpMux();
1614 }
1615 if (data_channel()) {
1616 data_channel()->ActivateRtcpMux();
1617 }
1618 }
1619
Donald Curtis0e209b02015-03-24 09:29:54 -07001620 // Enable bundle before when kMaxBundle policy is in effect.
1621 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
Peter Thatcher4eddf182015-04-30 10:55:59 -07001622 const cricket::ContentGroup* bundle_group = desc->GetGroupByName(
1623 cricket::GROUP_TYPE_BUNDLE);
1624 if (!bundle_group) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001625 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1626 return false;
1627 }
Peter Thatcher4eddf182015-04-30 10:55:59 -07001628 if (!BaseSession::BundleContentGroup(bundle_group)) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001629 LOG(LS_WARNING) << "max-bundle failed to enable bundling.";
1630 return false;
1631 }
1632 }
1633
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001634 return true;
1635}
1636
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001637bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001638 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
Jelena Marusicc28a8962015-05-29 15:05:44 +02001639 this, content->name, true, audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001640 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001641 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001642 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001643
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001644 voice_channel_->SignalDtlsSetupFailure.connect(
1645 this, &WebRtcSession::OnDtlsSetupFailure);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001646 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001647}
1648
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001649bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650 video_channel_.reset(channel_manager_->CreateVideoChannel(
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +00001651 this, content->name, true, video_options_, voice_channel_.get()));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001652 if (!video_channel_) {
1653 return false;
1654 }
1655
1656 video_channel_->SignalDtlsSetupFailure.connect(
1657 this, &WebRtcSession::OnDtlsSetupFailure);
1658 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001659}
1660
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001661bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001662 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001663 data_channel_.reset(channel_manager_->CreateDataChannel(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001664 this, content->name, !sctp, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001665 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001666 return false;
1667 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001668
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001669 if (sctp) {
1670 mediastream_signaling_->OnDataTransportCreatedForSctp();
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001671 data_channel_->SignalDataReceived.connect(
1672 this, &WebRtcSession::OnDataChannelMessageReceived);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00001673 data_channel_->SignalStreamClosedRemotely.connect(
1674 mediastream_signaling_,
1675 &MediaStreamSignaling::OnRemoteSctpDataChannelClosed);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001676 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001677
1678 data_channel_->SignalDtlsSetupFailure.connect(
1679 this, &WebRtcSession::OnDtlsSetupFailure);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001680 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001681}
1682
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001683void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
1684 SetError(BaseSession::ERROR_TRANSPORT, rtcp ? kDtlsSetupFailureRtcp :
1685 kDtlsSetupFailureRtp);
1686}
1687
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688void WebRtcSession::CopySavedCandidates(
1689 SessionDescriptionInterface* dest_desc) {
1690 if (!dest_desc) {
1691 ASSERT(false);
1692 return;
1693 }
1694 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
1695 dest_desc->AddCandidate(saved_candidates_[i]);
1696 delete saved_candidates_[i];
1697 }
1698 saved_candidates_.clear();
1699}
1700
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001701void WebRtcSession::OnDataChannelMessageReceived(
1702 cricket::DataChannel* channel,
1703 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001704 const rtc::Buffer& payload) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001705 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001706 if (params.type == cricket::DMT_CONTROL &&
1707 mediastream_signaling_->IsSctpSidAvailable(params.ssrc)) {
1708 // Received CONTROL on unused sid, process as an OPEN message.
1709 mediastream_signaling_->AddDataChannelFromOpenMessage(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001710 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001711 // otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001712}
1713
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001714// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001715bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001716 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1717 if (!bundle_enabled)
1718 return true;
1719
1720 const cricket::ContentGroup* bundle_group =
1721 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1722 ASSERT(bundle_group != NULL);
1723
1724 const cricket::ContentInfos& contents = desc->contents();
1725 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1726 citer != contents.end(); ++citer) {
1727 const cricket::ContentInfo* content = (&*citer);
1728 ASSERT(content != NULL);
1729 if (bundle_group->HasContentName(content->name) &&
1730 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1731 if (!HasRtcpMuxEnabled(content))
1732 return false;
1733 }
1734 }
1735 // RTCP-MUX is enabled in all the contents.
1736 return true;
1737}
1738
1739bool WebRtcSession::HasRtcpMuxEnabled(
1740 const cricket::ContentInfo* content) {
1741 const cricket::MediaContentDescription* description =
1742 static_cast<cricket::MediaContentDescription*>(content->description);
1743 return description->rtcp_mux();
1744}
1745
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001746bool WebRtcSession::ValidateSessionDescription(
1747 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001748 cricket::ContentSource source, std::string* err_desc) {
1749 std::string type;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001750 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001751 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001752 }
1753
1754 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001755 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001756 }
1757
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001758 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001759 Action action = GetAction(sdesc->type());
1760 if (source == cricket::CS_LOCAL) {
1761 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001762 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001763 } else {
1764 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001765 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001766 }
1767
1768 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001769 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001770 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1771 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001772 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001773 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001774 }
1775
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001776 // Verify ice-ufrag and ice-pwd.
1777 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001778 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001779 }
1780
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001781 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001782 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001783 }
1784
1785 // Verify m-lines in Answer when compared against Offer.
1786 if (action == kAnswer) {
1787 const cricket::SessionDescription* offer_desc =
1788 (source == cricket::CS_LOCAL) ? remote_description()->description() :
1789 local_description()->description();
1790 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001791 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001792 }
1793 }
1794
1795 return true;
1796}
1797
1798bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1799 return ((action == kOffer && state() == STATE_INIT) ||
1800 // update local offer
1801 (action == kOffer && state() == STATE_SENTINITIATE) ||
1802 // update the current ongoing session.
1803 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1804 (action == kOffer && state() == STATE_SENTACCEPT) ||
1805 (action == kOffer && state() == STATE_INPROGRESS) ||
1806 // accept remote offer
1807 (action == kAnswer && state() == STATE_RECEIVEDINITIATE) ||
1808 (action == kAnswer && state() == STATE_SENTPRACCEPT) ||
1809 (action == kPrAnswer && state() == STATE_RECEIVEDINITIATE) ||
1810 (action == kPrAnswer && state() == STATE_SENTPRACCEPT));
1811}
1812
1813bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1814 return ((action == kOffer && state() == STATE_INIT) ||
1815 // update remote offer
1816 (action == kOffer && state() == STATE_RECEIVEDINITIATE) ||
1817 // update the current ongoing session
1818 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1819 (action == kOffer && state() == STATE_SENTACCEPT) ||
1820 (action == kOffer && state() == STATE_INPROGRESS) ||
1821 // accept local offer
1822 (action == kAnswer && state() == STATE_SENTINITIATE) ||
1823 (action == kAnswer && state() == STATE_RECEIVEDPRACCEPT) ||
1824 (action == kPrAnswer && state() == STATE_SENTINITIATE) ||
1825 (action == kPrAnswer && state() == STATE_RECEIVEDPRACCEPT));
1826}
1827
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001828std::string WebRtcSession::GetSessionErrorMsg() {
1829 std::ostringstream desc;
1830 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1831 desc << kSessionErrorDesc << error_desc() << ".";
1832 return desc.str();
1833}
1834
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001835// We need to check the local/remote description for the Transport instead of
1836// the session, because a new Transport added during renegotiation may have
1837// them unset while the session has them set from the previous negotiation.
1838// Not doing so may trigger the auto generation of transport description and
1839// mess up DTLS identity information, ICE credential, etc.
1840bool WebRtcSession::ReadyToUseRemoteCandidate(
1841 const IceCandidateInterface* candidate,
1842 const SessionDescriptionInterface* remote_desc,
1843 bool* valid) {
1844 *valid = true;;
1845 cricket::TransportProxy* transport_proxy = NULL;
1846
1847 const SessionDescriptionInterface* current_remote_desc =
1848 remote_desc ? remote_desc : remote_description();
1849
1850 if (!current_remote_desc)
1851 return false;
1852
1853 size_t mediacontent_index =
1854 static_cast<size_t>(candidate->sdp_mline_index());
1855 size_t remote_content_size =
1856 current_remote_desc->description()->contents().size();
1857 if (mediacontent_index >= remote_content_size) {
1858 LOG(LS_ERROR)
1859 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
1860
1861 *valid = false;
1862 return false;
1863 }
1864
1865 cricket::ContentInfo content =
1866 current_remote_desc->description()->contents()[mediacontent_index];
1867 transport_proxy = GetTransportProxy(content.name);
1868
1869 return transport_proxy && transport_proxy->local_description_set() &&
1870 transport_proxy->remote_description_set();
1871}
1872
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001873// Walk through the ConnectionInfos to gather best connection usage
1874// for IPv4 and IPv6.
1875void WebRtcSession::ReportBestConnectionState(cricket::Transport* transport) {
1876 if (!metrics_observer_) {
1877 return;
1878 }
1879
1880 cricket::TransportStats stats;
1881 if (!transport->GetStats(&stats)) {
1882 return;
1883 }
1884
1885 for (cricket::TransportChannelStatsList::const_iterator it =
1886 stats.channel_stats.begin();
1887 it != stats.channel_stats.end(); ++it) {
1888 for (cricket::ConnectionInfos::const_iterator it_info =
1889 it->connection_infos.begin();
1890 it_info != it->connection_infos.end(); ++it_info) {
1891 if (!it_info->best_connection) {
1892 continue;
1893 }
1894 if (it_info->local_candidate.address().family() == AF_INET) {
1895 metrics_observer_->IncrementCounter(kBestConnections_IPv4);
1896 } else if (it_info->local_candidate.address().family() ==
1897 AF_INET6) {
1898 metrics_observer_->IncrementCounter(kBestConnections_IPv6);
1899 } else {
1900 ASSERT(false);
1901 }
1902 return;
1903 }
1904 }
1905}
1906
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001907} // namespace webrtc