blob: c2ce02c21a7df510dc1fb940f2354a04c440c98e [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();
508 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
509 }
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,
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000524 PeerConnectionInterface::IceTransportsType ice_transport_type,
525 PeerConnectionInterface::BundlePolicy bundle_policy) {
526 bundle_policy_ = bundle_policy;
527
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 // TODO(perkj): Take |constraints| into consideration. Return false if not all
529 // mandatory constraints can be fulfilled. Note that |constraints|
530 // can be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531 bool value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000532
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000533 if (options.disable_encryption) {
534 dtls_enabled_ = false;
535 } else {
536 // Enable DTLS by default if |dtls_identity_service| is valid.
537 dtls_enabled_ = (dtls_identity_service != NULL);
538 // |constraints| can override the default |dtls_enabled_| value.
539 if (FindConstraint(
540 constraints,
541 MediaConstraintsInterface::kEnableDtlsSrtp,
542 &value, NULL)) {
543 dtls_enabled_ = value;
544 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000545 }
546
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000548 // It takes precendence over the disable_sctp_data_channels
549 // PeerConnectionFactoryInterface::Options.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550 if (FindConstraint(
551 constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
552 &value, NULL) && value) {
553 LOG(LS_INFO) << "Allowing RTP data engine.";
554 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000555 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000556 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000557 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000558 LOG(LS_INFO) << "Allowing SCTP data engine.";
559 data_channel_type_ = cricket::DCT_SCTP;
560 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561 }
562 if (data_channel_type_ != cricket::DCT_NONE) {
563 mediastream_signaling_->SetDataChannelFactory(this);
564 }
565
wu@webrtc.orgde305012013-10-31 15:40:38 +0000566 // Find DSCP constraint.
567 if (FindConstraint(
568 constraints,
569 MediaConstraintsInterface::kEnableDscp,
570 &value, NULL)) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000571 audio_options_.dscp.Set(value);
572 video_options_.dscp.Set(value);
573 }
574
575 // Find Suspend Below Min Bitrate constraint.
576 if (FindConstraint(
577 constraints,
578 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
579 &value,
580 NULL)) {
581 video_options_.suspend_below_min_bitrate.Set(value);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000582 }
583
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000584 SetOptionFromOptionalConstraint(constraints,
585 MediaConstraintsInterface::kScreencastMinBitrate,
586 &video_options_.screencast_min_bitrate);
587
588 // Find constraints for cpu overuse detection.
589 SetOptionFromOptionalConstraint(constraints,
590 MediaConstraintsInterface::kCpuUnderuseThreshold,
591 &video_options_.cpu_underuse_threshold);
592 SetOptionFromOptionalConstraint(constraints,
593 MediaConstraintsInterface::kCpuOveruseThreshold,
594 &video_options_.cpu_overuse_threshold);
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000595 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000596 MediaConstraintsInterface::kCpuOveruseDetection,
597 &video_options_.cpu_overuse_detection);
598 SetOptionFromOptionalConstraint(constraints,
599 MediaConstraintsInterface::kCpuOveruseEncodeUsage,
600 &video_options_.cpu_overuse_encode_usage);
601 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000602 MediaConstraintsInterface::kCpuUnderuseEncodeRsdThreshold,
603 &video_options_.cpu_underuse_encode_rsd_threshold);
604 SetOptionFromOptionalConstraint(constraints,
605 MediaConstraintsInterface::kCpuOveruseEncodeRsdThreshold,
606 &video_options_.cpu_overuse_encode_rsd_threshold);
buildbot@webrtc.orgdb563902014-06-13 13:05:48 +0000607
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000608 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000609 MediaConstraintsInterface::kNumUnsignalledRecvStreams,
610 &video_options_.unsignalled_recv_stream_limit);
611 if (video_options_.unsignalled_recv_stream_limit.IsSet()) {
612 int stream_limit;
613 video_options_.unsignalled_recv_stream_limit.Get(&stream_limit);
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000614 stream_limit = std::min(kMaxUnsignalledRecvStreams, stream_limit);
615 stream_limit = std::max(0, stream_limit);
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000616 video_options_.unsignalled_recv_stream_limit.Set(stream_limit);
617 }
618
619 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000620 MediaConstraintsInterface::kHighStartBitrate,
621 &video_options_.video_start_bitrate);
wu@webrtc.orgcfe5e9c2014-03-27 17:03:58 +0000622
623 if (FindConstraint(
624 constraints,
625 MediaConstraintsInterface::kVeryHighBitrate,
626 &value,
627 NULL)) {
628 video_options_.video_highest_bitrate.Set(
629 cricket::VideoOptions::VERY_HIGH);
630 } else if (FindConstraint(
631 constraints,
632 MediaConstraintsInterface::kHighBitrate,
633 &value,
634 NULL)) {
635 video_options_.video_highest_bitrate.Set(
636 cricket::VideoOptions::HIGH);
637 }
638
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000639 SetOptionFromOptionalConstraint(constraints,
640 MediaConstraintsInterface::kCombinedAudioVideoBwe,
641 &audio_options_.combined_audio_video_bwe);
642
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643 const cricket::VideoCodec default_codec(
644 JsepSessionDescription::kDefaultVideoCodecId,
645 JsepSessionDescription::kDefaultVideoCodecName,
646 JsepSessionDescription::kMaxVideoCodecWidth,
647 JsepSessionDescription::kMaxVideoCodecHeight,
648 JsepSessionDescription::kDefaultVideoCodecFramerate,
649 JsepSessionDescription::kDefaultVideoCodecPreference);
650 channel_manager_->SetDefaultVideoEncoderConfig(
651 cricket::VideoEncoderConfig(default_codec));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000652
653 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
654 signaling_thread(),
655 channel_manager_,
656 mediastream_signaling_,
657 dtls_identity_service,
658 this,
659 id(),
660 data_channel_type_,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000661 dtls_enabled_));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000662
663 webrtc_session_desc_factory_->SignalIdentityReady.connect(
664 this, &WebRtcSession::OnIdentityReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000665
wu@webrtc.org97077a32013-10-25 21:18:33 +0000666 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000667 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000668 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000669 port_allocator()->set_candidate_filter(
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000670 ConvertIceTransportTypeToCandidateFilter(ice_transport_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000671 return true;
672}
673
674void WebRtcSession::Terminate() {
675 SetState(STATE_RECEIVEDTERMINATE);
676 RemoveUnusedChannelsAndTransports(NULL);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000677 ASSERT(!voice_channel_);
678 ASSERT(!video_channel_);
679 ASSERT(!data_channel_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000680}
681
682bool WebRtcSession::StartCandidatesAllocation() {
683 // SpeculativelyConnectTransportChannels, will call ConnectChannels method
684 // from TransportProxy to start gathering ice candidates.
685 SpeculativelyConnectAllTransportChannels();
686 if (!saved_candidates_.empty()) {
687 // If there are saved candidates which arrived before local description is
688 // set, copy those to remote description.
689 CopySavedCandidates(remote_desc_.get());
690 }
691 // Push remote candidates present in remote description to transport channels.
692 UseCandidatesInSessionDescription(remote_desc_.get());
693 return true;
694}
695
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000696void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
697 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698}
699
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000700cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
701 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702}
703
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000704bool WebRtcSession::GetSslRole(rtc::SSLRole* role) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000705 if (local_description() == NULL || remote_description() == NULL) {
706 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
707 << "SSL Role of the session.";
708 return false;
709 }
710
711 // TODO(mallinath) - Return role of each transport, as role may differ from
712 // one another.
713 // In current implementaion we just return the role of first transport in the
714 // transport map.
715 for (cricket::TransportMap::const_iterator iter = transport_proxies().begin();
716 iter != transport_proxies().end(); ++iter) {
717 if (iter->second->impl()) {
718 return iter->second->impl()->GetSslRole(role);
719 }
720 }
721 return false;
722}
723
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000724void WebRtcSession::CreateOffer(
725 CreateSessionDescriptionObserver* observer,
726 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
727 webrtc_session_desc_factory_->CreateOffer(observer, options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000728}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729
wu@webrtc.org91053e72013-08-10 07:18:04 +0000730void WebRtcSession::CreateAnswer(CreateSessionDescriptionObserver* observer,
731 const MediaConstraintsInterface* constraints) {
732 webrtc_session_desc_factory_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733}
734
735bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
736 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000737 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000738 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000739
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000740 // Validate SDP.
741 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
742 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743 }
744
745 // Update the initiator flag if this session is the initiator.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000746 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 if (state() == STATE_INIT && action == kOffer) {
748 set_initiator(true);
749 }
750
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000751 cricket::SecurePolicy sdes_policy =
752 webrtc_session_desc_factory_->SdesPolicy();
753 cricket::CryptoType crypto_required = dtls_enabled_ ?
754 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
755 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000757 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758
759 set_local_description(desc->description()->Copy());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000760 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761
762 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000763 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764 // TODO(mallinath) - Handle CreateChannel failure, as new local description
765 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000766 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767 }
768
769 // Remove channel and transport proxies, if MediaContentDescription is
770 // rejected.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000771 RemoveUnusedChannelsAndTransports(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000773 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000774 return false;
775 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000776
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777 // Kick starting the ice candidates allocation.
778 StartCandidatesAllocation();
779
780 // Update state and SSRC of local MediaStreams and DataChannels based on the
781 // local session description.
782 mediastream_signaling_->OnLocalDescriptionChanged(local_desc_.get());
783
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000784 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000785 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
786 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
787 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000789 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 }
791 return true;
792}
793
794bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
795 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000796 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000797 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000798
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000799 // Validate SDP.
800 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
801 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802 }
803
804 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000805 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 if (action == kOffer && !CreateChannels(desc->description())) {
807 // TODO(mallinath) - Handle CreateChannel failure, as new local description
808 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000809 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810 }
811
812 // Remove channel and transport proxies, if MediaContentDescription is
813 // rejected.
814 RemoveUnusedChannelsAndTransports(desc->description());
815
816 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
817 // is called.
818 set_remote_description(desc->description()->Copy());
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000819 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 return false;
821 }
822
823 // Update remote MediaStreams.
824 mediastream_signaling_->OnRemoteDescriptionChanged(desc);
825 if (local_description() && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000826 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 }
828
829 // Copy all saved candidates.
830 CopySavedCandidates(desc);
831 // We retain all received candidates.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000832 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
833 remote_desc_.get(), desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000834 // Check if this new SessionDescription contains new ice ufrag and password
835 // that indicates the remote peer requests ice restart.
836 ice_restart_latch_->CheckForRemoteIceRestart(remote_desc_.get(),
837 desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000838 remote_desc_.reset(desc_temp.release());
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000839
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000840 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000841 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
842 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
843 }
844
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000845 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000846 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000848
849 // Set the the ICE connection state to connecting since the connection may
850 // become writable with peer reflexive candidates before any remote candidate
851 // is signaled.
852 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
853 // is to have a new signal the indicates a change in checking state from the
854 // transport and expose a new checking() member from transport that can be
855 // read to determine the current checking state. The existing SignalConnecting
856 // actually means "gathering candidates", so cannot be be used here.
857 if (desc->type() != SessionDescriptionInterface::kOffer &&
858 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
859 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
860 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000861 return true;
862}
863
864bool WebRtcSession::UpdateSessionState(
865 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000866 std::string* err_desc) {
867 // If there's already a pending error then no state transition should happen.
868 // But all call-sites should be verifying this before calling us!
869 ASSERT(error() == cricket::BaseSession::ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000870 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000871 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000872 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
873 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000874 }
875 SetState(source == cricket::CS_LOCAL ?
876 STATE_SENTINITIATE : STATE_RECEIVEDINITIATE);
877 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000878 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879 }
880 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000881 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
882 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 }
884 EnableChannels();
885 SetState(source == cricket::CS_LOCAL ?
886 STATE_SENTPRACCEPT : STATE_RECEIVEDPRACCEPT);
887 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000888 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889 }
890 } else if (action == kAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000891 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
892 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893 }
894 MaybeEnableMuxingSupport();
895 EnableChannels();
896 SetState(source == cricket::CS_LOCAL ?
897 STATE_SENTACCEPT : STATE_RECEIVEDACCEPT);
898 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000899 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 }
901 }
902 return true;
903}
904
905WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
906 if (type == SessionDescriptionInterface::kOffer) {
907 return WebRtcSession::kOffer;
908 } else if (type == SessionDescriptionInterface::kPrAnswer) {
909 return WebRtcSession::kPrAnswer;
910 } else if (type == SessionDescriptionInterface::kAnswer) {
911 return WebRtcSession::kAnswer;
912 }
913 ASSERT(false && "unknown action type");
914 return WebRtcSession::kOffer;
915}
916
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000917bool WebRtcSession::GetTransportStats(cricket::SessionStats* stats) {
918 ASSERT(signaling_thread()->IsCurrent());
919
920 const auto get_transport_stats = [stats](const std::string& content_name,
921 cricket::Transport* transport) {
922 const std::string& transport_id = transport->content_name();
923 stats->proxy_to_transport[content_name] = transport_id;
924 if (stats->transport_stats.find(transport_id)
925 != stats->transport_stats.end()) {
926 // Transport stats already done for this transport.
927 return true;
928 }
929
930 cricket::TransportStats tstats;
931 if (!transport->GetStats(&tstats)) {
932 return false;
933 }
934
935 stats->transport_stats[transport_id] = tstats;
936 return true;
937 };
938
939 for (const auto& kv : transport_proxies()) {
940 cricket::Transport* transport = kv.second->impl();
941 if (transport && !get_transport_stats(kv.first, transport)) {
942 return false;
943 }
944 }
945 return true;
946}
947
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000948bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
949 if (state() == STATE_INIT) {
950 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
951 << "without any offer (local or remote) "
952 << "session description.";
953 return false;
954 }
955
956 if (!candidate) {
957 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL";
958 return false;
959 }
960
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +0000961 bool valid = false;
962 if (!ReadyToUseRemoteCandidate(candidate, NULL, &valid)) {
963 if (valid) {
964 LOG(LS_INFO) << "ProcessIceMessage: Candidate saved";
965 saved_candidates_.push_back(
966 new JsepIceCandidate(candidate->sdp_mid(),
967 candidate->sdp_mline_index(),
968 candidate->candidate()));
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000969 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +0000970 return valid;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971 }
972
973 // Add this candidate to the remote session description.
974 if (!remote_desc_->AddCandidate(candidate)) {
975 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used";
976 return false;
977 }
978
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000979 return UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000980}
981
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000982bool WebRtcSession::SetIceTransports(
983 PeerConnectionInterface::IceTransportsType type) {
984 return port_allocator()->set_candidate_filter(
985 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000986}
987
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000988bool WebRtcSession::GetLocalTrackIdBySsrc(uint32 ssrc, std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989 if (!BaseSession::local_description())
990 return false;
991 return webrtc::GetTrackIdBySsrc(
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000992 BaseSession::local_description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000993}
994
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000995bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32 ssrc, std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000996 if (!BaseSession::remote_description())
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000997 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998 return webrtc::GetTrackIdBySsrc(
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000999 BaseSession::remote_description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001000}
1001
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001002std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001003 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001004 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001005 return desc.str();
1006}
1007
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001008void WebRtcSession::SetAudioPlayout(uint32 ssrc, bool enable,
1009 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001010 ASSERT(signaling_thread()->IsCurrent());
1011 if (!voice_channel_) {
1012 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
1013 return;
1014 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001015 if (!voice_channel_->SetRemoteRenderer(ssrc, renderer)) {
1016 // SetRenderer() can fail if the ssrc does not match any playout channel.
1017 LOG(LS_ERROR) << "SetAudioPlayout: ssrc is incorrect: " << ssrc;
1018 return;
1019 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001020 if (!voice_channel_->SetOutputScaling(ssrc, enable ? 1 : 0, enable ? 1 : 0)) {
1021 // Allow that SetOutputScaling fail if |enable| is false but assert
1022 // otherwise. This in the normal case when the underlying media channel has
1023 // already been deleted.
1024 ASSERT(enable == false);
1025 }
1026}
1027
1028void WebRtcSession::SetAudioSend(uint32 ssrc, bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001029 const cricket::AudioOptions& options,
1030 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001031 ASSERT(signaling_thread()->IsCurrent());
1032 if (!voice_channel_) {
1033 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
1034 return;
1035 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001036 if (!voice_channel_->SetLocalRenderer(ssrc, renderer)) {
1037 // SetRenderer() can fail if the ssrc does not match any send channel.
1038 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
1039 return;
1040 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041 if (!voice_channel_->MuteStream(ssrc, !enable)) {
1042 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1043 // This in the normal case when the underlying media channel has already
1044 // been deleted.
1045 ASSERT(enable == false);
1046 return;
1047 }
1048 if (enable)
1049 voice_channel_->SetChannelOptions(options);
1050}
1051
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001052void WebRtcSession::SetAudioPlayoutVolume(uint32 ssrc, double volume) {
1053 ASSERT(signaling_thread()->IsCurrent());
1054 ASSERT(volume >= 0 && volume <= 10);
1055 if (!voice_channel_) {
1056 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1057 return;
1058 }
1059
1060 if (!voice_channel_->SetOutputScaling(ssrc, volume, volume))
1061 ASSERT(false);
1062}
1063
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001064bool WebRtcSession::SetCaptureDevice(uint32 ssrc,
1065 cricket::VideoCapturer* camera) {
1066 ASSERT(signaling_thread()->IsCurrent());
1067
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001068 if (!video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001069 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1070 // support video.
1071 LOG(LS_WARNING) << "Video not used in this call.";
1072 return false;
1073 }
1074 if (!video_channel_->SetCapturer(ssrc, camera)) {
1075 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1076 // This in the normal case when the underlying media channel has already
1077 // been deleted.
1078 ASSERT(camera == NULL);
1079 return false;
1080 }
1081 return true;
1082}
1083
1084void WebRtcSession::SetVideoPlayout(uint32 ssrc,
1085 bool enable,
1086 cricket::VideoRenderer* renderer) {
1087 ASSERT(signaling_thread()->IsCurrent());
1088 if (!video_channel_) {
1089 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1090 return;
1091 }
1092 if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) {
1093 // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise.
1094 // This in the normal case when the underlying media channel has already
1095 // been deleted.
1096 ASSERT(renderer == NULL);
1097 }
1098}
1099
1100void WebRtcSession::SetVideoSend(uint32 ssrc, bool enable,
1101 const cricket::VideoOptions* options) {
1102 ASSERT(signaling_thread()->IsCurrent());
1103 if (!video_channel_) {
1104 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1105 return;
1106 }
1107 if (!video_channel_->MuteStream(ssrc, !enable)) {
1108 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1109 // This in the normal case when the underlying media channel has already
1110 // been deleted.
1111 ASSERT(enable == false);
1112 return;
1113 }
1114 if (enable && options)
1115 video_channel_->SetChannelOptions(*options);
1116}
1117
1118bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1119 ASSERT(signaling_thread()->IsCurrent());
1120 if (!voice_channel_) {
1121 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1122 return false;
1123 }
1124 uint32 send_ssrc = 0;
1125 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1126 // exists.
1127 if (!GetAudioSsrcByTrackId(BaseSession::local_description(), track_id,
1128 &send_ssrc)) {
1129 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1130 return false;
1131 }
1132 return voice_channel_->CanInsertDtmf();
1133}
1134
1135bool WebRtcSession::InsertDtmf(const std::string& track_id,
1136 int code, int duration) {
1137 ASSERT(signaling_thread()->IsCurrent());
1138 if (!voice_channel_) {
1139 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1140 return false;
1141 }
1142 uint32 send_ssrc = 0;
1143 if (!VERIFY(GetAudioSsrcByTrackId(BaseSession::local_description(),
1144 track_id, &send_ssrc))) {
1145 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1146 return false;
1147 }
1148 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration,
1149 cricket::DF_SEND)) {
1150 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1151 return false;
1152 }
1153 return true;
1154}
1155
1156sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
1157 return &SignalVoiceChannelDestroyed;
1158}
1159
wu@webrtc.org78187522013-10-07 23:32:02 +00001160bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001161 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001162 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001163 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001164 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1165 return false;
1166 }
1167 return data_channel_->SendData(params, payload, result);
1168}
1169
1170bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001171 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001172 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1173 return false;
1174 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001175 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1176 &DataChannel::OnChannelReady);
1177 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1178 &DataChannel::OnDataReceived);
wu@webrtc.org78187522013-10-07 23:32:02 +00001179 return true;
1180}
1181
1182void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001183 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001184 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1185 return;
1186 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001187 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1188 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
1189}
1190
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001191void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001192 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001193 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1194 return;
1195 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001196 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1197 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001198}
1199
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001200void WebRtcSession::RemoveSctpDataStream(int sid) {
1201 mediastream_signaling_->RemoveSctpDataChannel(sid);
jiayl@webrtc.org2eaac182014-06-17 16:02:46 +00001202
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001203 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001204 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1205 << "NULL.";
1206 return;
1207 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001208 data_channel_->RemoveRecvStream(sid);
1209 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001210}
1211
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001212bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001213 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001214}
1215
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001216rtc::scoped_refptr<DataChannel> WebRtcSession::CreateDataChannel(
wu@webrtc.org78187522013-10-07 23:32:02 +00001217 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001218 const InternalDataChannelInit* config) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001219 if (state() == STATE_RECEIVEDTERMINATE) {
1220 return NULL;
1221 }
1222 if (data_channel_type_ == cricket::DCT_NONE) {
1223 LOG(LS_ERROR) << "CreateDataChannel: Data is not supported in this call.";
1224 return NULL;
1225 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001226 InternalDataChannelInit new_config =
1227 config ? (*config) : InternalDataChannelInit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001228 if (data_channel_type_ == cricket::DCT_SCTP) {
1229 if (new_config.id < 0) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001230 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001231 if (GetSslRole(&role) &&
1232 !mediastream_signaling_->AllocateSctpSid(role, &new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001233 LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel.";
1234 return NULL;
1235 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001236 } else if (!mediastream_signaling_->IsSctpSidAvailable(new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001237 LOG(LS_ERROR) << "Failed to create a SCTP data channel "
1238 << "because the id is already in use or out of range.";
1239 return NULL;
1240 }
1241 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001242
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001243 rtc::scoped_refptr<DataChannel> channel(DataChannel::Create(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001244 this, data_channel_type_, label, new_config));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001245 if (channel && !mediastream_signaling_->AddDataChannel(channel))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001246 return NULL;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001247
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001248 return channel;
1249}
1250
1251cricket::DataChannelType WebRtcSession::data_channel_type() const {
1252 return data_channel_type_;
1253}
1254
wu@webrtc.org91053e72013-08-10 07:18:04 +00001255bool WebRtcSession::IceRestartPending() const {
1256 return ice_restart_latch_->Get();
1257}
1258
1259void WebRtcSession::ResetIceRestartLatch() {
1260 ice_restart_latch_->Reset();
1261}
1262
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001263void WebRtcSession::OnIdentityReady(rtc::SSLIdentity* identity) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001264 SetIdentity(identity);
1265}
1266
1267bool WebRtcSession::waiting_for_identity() const {
1268 return webrtc_session_desc_factory_->waiting_for_identity();
1269}
1270
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001271void WebRtcSession::SetIceConnectionState(
1272 PeerConnectionInterface::IceConnectionState state) {
1273 if (ice_connection_state_ == state) {
1274 return;
1275 }
1276
1277 // ASSERT that the requested transition is allowed. Note that
1278 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1279 // within PeerConnection). This switch statement should compile away when
1280 // ASSERTs are disabled.
1281 switch (ice_connection_state_) {
1282 case PeerConnectionInterface::kIceConnectionNew:
1283 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1284 break;
1285 case PeerConnectionInterface::kIceConnectionChecking:
1286 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1287 state == PeerConnectionInterface::kIceConnectionConnected);
1288 break;
1289 case PeerConnectionInterface::kIceConnectionConnected:
1290 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1291 state == PeerConnectionInterface::kIceConnectionChecking ||
1292 state == PeerConnectionInterface::kIceConnectionCompleted);
1293 break;
1294 case PeerConnectionInterface::kIceConnectionCompleted:
1295 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1296 state == PeerConnectionInterface::kIceConnectionDisconnected);
1297 break;
1298 case PeerConnectionInterface::kIceConnectionFailed:
1299 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1300 break;
1301 case PeerConnectionInterface::kIceConnectionDisconnected:
1302 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1303 state == PeerConnectionInterface::kIceConnectionConnected ||
1304 state == PeerConnectionInterface::kIceConnectionCompleted ||
1305 state == PeerConnectionInterface::kIceConnectionFailed);
1306 break;
1307 case PeerConnectionInterface::kIceConnectionClosed:
1308 ASSERT(false);
1309 break;
1310 default:
1311 ASSERT(false);
1312 break;
1313 }
1314
1315 ice_connection_state_ = state;
1316 if (ice_observer_) {
1317 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1318 }
1319}
1320
1321void WebRtcSession::OnTransportRequestSignaling(
1322 cricket::Transport* transport) {
1323 ASSERT(signaling_thread()->IsCurrent());
1324 transport->OnSignalingReady();
1325 if (ice_observer_) {
1326 ice_observer_->OnIceGatheringChange(
1327 PeerConnectionInterface::kIceGatheringGathering);
1328 }
1329}
1330
1331void WebRtcSession::OnTransportConnecting(cricket::Transport* transport) {
1332 ASSERT(signaling_thread()->IsCurrent());
1333 // start monitoring for the write state of the transport.
1334 OnTransportWritable(transport);
1335}
1336
1337void WebRtcSession::OnTransportWritable(cricket::Transport* transport) {
1338 ASSERT(signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001339 if (transport->all_channels_writable()) {
henrike@webrtc.org05376342014-03-10 15:53:12 +00001340 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001341 } else if (transport->HasChannels()) {
1342 // If the current state is Connected or Completed, then there were writable
1343 // channels but now there are not, so the next state must be Disconnected.
1344 if (ice_connection_state_ ==
1345 PeerConnectionInterface::kIceConnectionConnected ||
1346 ice_connection_state_ ==
1347 PeerConnectionInterface::kIceConnectionCompleted) {
1348 SetIceConnectionState(
1349 PeerConnectionInterface::kIceConnectionDisconnected);
1350 }
1351 }
1352}
1353
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001354void WebRtcSession::OnTransportCompleted(cricket::Transport* transport) {
1355 ASSERT(signaling_thread()->IsCurrent());
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001356 PeerConnectionInterface::IceConnectionState old_state = ice_connection_state_;
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001357 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001358 // Only report once when Ice connection is completed.
1359 if (old_state != PeerConnectionInterface::kIceConnectionCompleted) {
1360 ReportBestConnectionState(transport);
1361 }
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001362}
1363
1364void WebRtcSession::OnTransportFailed(cricket::Transport* transport) {
1365 ASSERT(signaling_thread()->IsCurrent());
1366 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1367}
1368
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001369void WebRtcSession::OnTransportProxyCandidatesReady(
1370 cricket::TransportProxy* proxy, const cricket::Candidates& candidates) {
1371 ASSERT(signaling_thread()->IsCurrent());
1372 ProcessNewLocalCandidate(proxy->content_name(), candidates);
1373}
1374
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001375void WebRtcSession::OnCandidatesAllocationDone() {
1376 ASSERT(signaling_thread()->IsCurrent());
1377 if (ice_observer_) {
1378 ice_observer_->OnIceGatheringChange(
1379 PeerConnectionInterface::kIceGatheringComplete);
1380 ice_observer_->OnIceComplete();
1381 }
1382}
1383
1384// Enabling voice and video channel.
1385void WebRtcSession::EnableChannels() {
1386 if (voice_channel_ && !voice_channel_->enabled())
1387 voice_channel_->Enable(true);
1388
1389 if (video_channel_ && !video_channel_->enabled())
1390 video_channel_->Enable(true);
1391
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001392 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001393 data_channel_->Enable(true);
1394}
1395
1396void WebRtcSession::ProcessNewLocalCandidate(
1397 const std::string& content_name,
1398 const cricket::Candidates& candidates) {
1399 int sdp_mline_index;
1400 if (!GetLocalCandidateMediaIndex(content_name, &sdp_mline_index)) {
1401 LOG(LS_ERROR) << "ProcessNewLocalCandidate: content name "
1402 << content_name << " not found";
1403 return;
1404 }
1405
1406 for (cricket::Candidates::const_iterator citer = candidates.begin();
1407 citer != candidates.end(); ++citer) {
1408 // Use content_name as the candidate media id.
1409 JsepIceCandidate candidate(content_name, sdp_mline_index, *citer);
1410 if (ice_observer_) {
1411 ice_observer_->OnIceCandidate(&candidate);
1412 }
1413 if (local_desc_) {
1414 local_desc_->AddCandidate(&candidate);
1415 }
1416 }
1417}
1418
1419// Returns the media index for a local ice candidate given the content name.
1420bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1421 int* sdp_mline_index) {
1422 if (!BaseSession::local_description() || !sdp_mline_index)
1423 return false;
1424
1425 bool content_found = false;
1426 const ContentInfos& contents = BaseSession::local_description()->contents();
1427 for (size_t index = 0; index < contents.size(); ++index) {
1428 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001429 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001430 content_found = true;
1431 break;
1432 }
1433 }
1434 return content_found;
1435}
1436
1437bool WebRtcSession::UseCandidatesInSessionDescription(
1438 const SessionDescriptionInterface* remote_desc) {
1439 if (!remote_desc)
1440 return true;
1441 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001442
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001443 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1444 const IceCandidateCollection* candidates = remote_desc->candidates(m);
1445 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001446 const IceCandidateInterface* candidate = candidates->at(n);
1447 bool valid = false;
1448 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1449 if (valid) {
1450 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Candidate saved.";
1451 saved_candidates_.push_back(
1452 new JsepIceCandidate(candidate->sdp_mid(),
1453 candidate->sdp_mline_index(),
1454 candidate->candidate()));
1455 }
1456 continue;
1457 }
1458
1459 ret = UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001460 if (!ret)
1461 break;
1462 }
1463 }
1464 return ret;
1465}
1466
1467bool WebRtcSession::UseCandidate(
1468 const IceCandidateInterface* candidate) {
1469
1470 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
1471 size_t remote_content_size =
1472 BaseSession::remote_description()->contents().size();
1473 if (mediacontent_index >= remote_content_size) {
1474 LOG(LS_ERROR)
1475 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1476 return false;
1477 }
1478
1479 cricket::ContentInfo content =
1480 BaseSession::remote_description()->contents()[mediacontent_index];
1481 std::vector<cricket::Candidate> candidates;
1482 candidates.push_back(candidate->candidate());
1483 // Invoking BaseSession method to handle remote candidates.
1484 std::string error;
1485 if (OnRemoteCandidates(content.name, candidates, &error)) {
1486 // Candidates successfully submitted for checking.
1487 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1488 ice_connection_state_ ==
1489 PeerConnectionInterface::kIceConnectionDisconnected) {
1490 // If state is New, then the session has just gotten its first remote ICE
1491 // candidates, so go to Checking.
1492 // If state is Disconnected, the session is re-using old candidates or
1493 // receiving additional ones, so go to Checking.
1494 // If state is Connected, stay Connected.
1495 // TODO(bemasc): If state is Connected, and the new candidates are for a
1496 // newly added transport, then the state actually _should_ move to
1497 // checking. Add a way to distinguish that case.
1498 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1499 }
1500 // TODO(bemasc): If state is Completed, go back to Connected.
1501 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001502 if (!error.empty()) {
1503 LOG(LS_WARNING) << error;
1504 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001505 }
1506 return true;
1507}
1508
1509void WebRtcSession::RemoveUnusedChannelsAndTransports(
1510 const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001511 // Destroy video_channel_ first since it may have a pointer to the
1512 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001513 const cricket::ContentInfo* video_info =
1514 cricket::GetFirstVideoContent(desc);
1515 if ((!video_info || video_info->rejected) && video_channel_) {
1516 mediastream_signaling_->OnVideoChannelClose();
1517 SignalVideoChannelDestroyed();
1518 const std::string content_name = video_channel_->content_name();
1519 channel_manager_->DestroyVideoChannel(video_channel_.release());
bjornv@webrtc.org520a69e2015-02-04 12:45:44 +00001520 DestroyTransportProxy(content_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001521 }
1522
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001523 const cricket::ContentInfo* voice_info =
1524 cricket::GetFirstAudioContent(desc);
1525 if ((!voice_info || voice_info->rejected) && voice_channel_) {
1526 mediastream_signaling_->OnAudioChannelClose();
1527 SignalVoiceChannelDestroyed();
1528 const std::string content_name = voice_channel_->content_name();
1529 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
bjornv@webrtc.org520a69e2015-02-04 12:45:44 +00001530 DestroyTransportProxy(content_name);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001531 }
1532
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001533 const cricket::ContentInfo* data_info =
1534 cricket::GetFirstDataContent(desc);
1535 if ((!data_info || data_info->rejected) && data_channel_) {
1536 mediastream_signaling_->OnDataChannelClose();
1537 SignalDataChannelDestroyed();
1538 const std::string content_name = data_channel_->content_name();
1539 channel_manager_->DestroyDataChannel(data_channel_.release());
bjornv@webrtc.org520a69e2015-02-04 12:45:44 +00001540 DestroyTransportProxy(content_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001541 }
1542}
1543
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001544// TODO(mallinath) - Add a correct error code if the channels are not creatued
1545// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001546bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
1547 // Disabling the BUNDLE flag in PortAllocator if offer disabled it.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001548 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1549 if (state() == STATE_INIT && !bundle_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001550 port_allocator()->set_flags(port_allocator()->flags() &
1551 ~cricket::PORTALLOCATOR_ENABLE_BUNDLE);
1552 }
1553
1554 // Creating the media channels and transport proxies.
1555 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1556 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001557 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001558 LOG(LS_ERROR) << "Failed to create voice channel.";
1559 return false;
1560 }
1561 }
1562
1563 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1564 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001565 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001566 LOG(LS_ERROR) << "Failed to create video channel.";
1567 return false;
1568 }
1569 }
1570
1571 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1572 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001573 data && !data->rejected && !data_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001574 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001575 LOG(LS_ERROR) << "Failed to create data channel.";
1576 return false;
1577 }
1578 }
1579
1580 return true;
1581}
1582
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001583bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001585 this, content->name, true));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001586 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001587 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001588 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001589
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00001590 voice_channel_->SetChannelOptions(audio_options_);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001591 voice_channel_->SignalDtlsSetupFailure.connect(
1592 this, &WebRtcSession::OnDtlsSetupFailure);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001593 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594}
1595
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001596bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001597 video_channel_.reset(channel_manager_->CreateVideoChannel(
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +00001598 this, content->name, true, video_options_, voice_channel_.get()));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001599 if (!video_channel_) {
1600 return false;
1601 }
1602
1603 video_channel_->SignalDtlsSetupFailure.connect(
1604 this, &WebRtcSession::OnDtlsSetupFailure);
1605 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001606}
1607
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001608bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001609 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001610 data_channel_.reset(channel_manager_->CreateDataChannel(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001611 this, content->name, !sctp, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001612 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001613 return false;
1614 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001615
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001616 if (sctp) {
1617 mediastream_signaling_->OnDataTransportCreatedForSctp();
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001618 data_channel_->SignalDataReceived.connect(
1619 this, &WebRtcSession::OnDataChannelMessageReceived);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00001620 data_channel_->SignalStreamClosedRemotely.connect(
1621 mediastream_signaling_,
1622 &MediaStreamSignaling::OnRemoteSctpDataChannelClosed);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001623 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001624
1625 data_channel_->SignalDtlsSetupFailure.connect(
1626 this, &WebRtcSession::OnDtlsSetupFailure);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001627 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628}
1629
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001630void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
1631 SetError(BaseSession::ERROR_TRANSPORT, rtcp ? kDtlsSetupFailureRtcp :
1632 kDtlsSetupFailureRtp);
1633}
1634
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001635void WebRtcSession::CopySavedCandidates(
1636 SessionDescriptionInterface* dest_desc) {
1637 if (!dest_desc) {
1638 ASSERT(false);
1639 return;
1640 }
1641 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
1642 dest_desc->AddCandidate(saved_candidates_[i]);
1643 delete saved_candidates_[i];
1644 }
1645 saved_candidates_.clear();
1646}
1647
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001648void WebRtcSession::OnDataChannelMessageReceived(
1649 cricket::DataChannel* channel,
1650 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001651 const rtc::Buffer& payload) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001652 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001653 if (params.type == cricket::DMT_CONTROL &&
1654 mediastream_signaling_->IsSctpSidAvailable(params.ssrc)) {
1655 // Received CONTROL on unused sid, process as an OPEN message.
1656 mediastream_signaling_->AddDataChannelFromOpenMessage(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001657 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001658 // otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001659}
1660
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001661// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001662bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001663 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1664 if (!bundle_enabled)
1665 return true;
1666
1667 const cricket::ContentGroup* bundle_group =
1668 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1669 ASSERT(bundle_group != NULL);
1670
1671 const cricket::ContentInfos& contents = desc->contents();
1672 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1673 citer != contents.end(); ++citer) {
1674 const cricket::ContentInfo* content = (&*citer);
1675 ASSERT(content != NULL);
1676 if (bundle_group->HasContentName(content->name) &&
1677 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1678 if (!HasRtcpMuxEnabled(content))
1679 return false;
1680 }
1681 }
1682 // RTCP-MUX is enabled in all the contents.
1683 return true;
1684}
1685
1686bool WebRtcSession::HasRtcpMuxEnabled(
1687 const cricket::ContentInfo* content) {
1688 const cricket::MediaContentDescription* description =
1689 static_cast<cricket::MediaContentDescription*>(content->description);
1690 return description->rtcp_mux();
1691}
1692
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001693bool WebRtcSession::ValidateSessionDescription(
1694 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001695 cricket::ContentSource source, std::string* err_desc) {
1696 std::string type;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001697 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001698 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001699 }
1700
1701 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001702 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001703 }
1704
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001705 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001706 Action action = GetAction(sdesc->type());
1707 if (source == cricket::CS_LOCAL) {
1708 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001709 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001710 } else {
1711 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001712 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001713 }
1714
1715 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001716 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001717 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1718 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001719 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001720 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001721 }
1722
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001723 // Verify ice-ufrag and ice-pwd.
1724 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001725 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001726 }
1727
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001728 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001729 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001730 }
1731
1732 // Verify m-lines in Answer when compared against Offer.
1733 if (action == kAnswer) {
1734 const cricket::SessionDescription* offer_desc =
1735 (source == cricket::CS_LOCAL) ? remote_description()->description() :
1736 local_description()->description();
1737 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001738 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001739 }
1740 }
1741
1742 return true;
1743}
1744
1745bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1746 return ((action == kOffer && state() == STATE_INIT) ||
1747 // update local offer
1748 (action == kOffer && state() == STATE_SENTINITIATE) ||
1749 // update the current ongoing session.
1750 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1751 (action == kOffer && state() == STATE_SENTACCEPT) ||
1752 (action == kOffer && state() == STATE_INPROGRESS) ||
1753 // accept remote offer
1754 (action == kAnswer && state() == STATE_RECEIVEDINITIATE) ||
1755 (action == kAnswer && state() == STATE_SENTPRACCEPT) ||
1756 (action == kPrAnswer && state() == STATE_RECEIVEDINITIATE) ||
1757 (action == kPrAnswer && state() == STATE_SENTPRACCEPT));
1758}
1759
1760bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1761 return ((action == kOffer && state() == STATE_INIT) ||
1762 // update remote offer
1763 (action == kOffer && state() == STATE_RECEIVEDINITIATE) ||
1764 // update the current ongoing session
1765 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1766 (action == kOffer && state() == STATE_SENTACCEPT) ||
1767 (action == kOffer && state() == STATE_INPROGRESS) ||
1768 // accept local offer
1769 (action == kAnswer && state() == STATE_SENTINITIATE) ||
1770 (action == kAnswer && state() == STATE_RECEIVEDPRACCEPT) ||
1771 (action == kPrAnswer && state() == STATE_SENTINITIATE) ||
1772 (action == kPrAnswer && state() == STATE_RECEIVEDPRACCEPT));
1773}
1774
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001775std::string WebRtcSession::GetSessionErrorMsg() {
1776 std::ostringstream desc;
1777 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1778 desc << kSessionErrorDesc << error_desc() << ".";
1779 return desc.str();
1780}
1781
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001782// We need to check the local/remote description for the Transport instead of
1783// the session, because a new Transport added during renegotiation may have
1784// them unset while the session has them set from the previous negotiation.
1785// Not doing so may trigger the auto generation of transport description and
1786// mess up DTLS identity information, ICE credential, etc.
1787bool WebRtcSession::ReadyToUseRemoteCandidate(
1788 const IceCandidateInterface* candidate,
1789 const SessionDescriptionInterface* remote_desc,
1790 bool* valid) {
1791 *valid = true;;
1792 cricket::TransportProxy* transport_proxy = NULL;
1793
1794 const SessionDescriptionInterface* current_remote_desc =
1795 remote_desc ? remote_desc : remote_description();
1796
1797 if (!current_remote_desc)
1798 return false;
1799
1800 size_t mediacontent_index =
1801 static_cast<size_t>(candidate->sdp_mline_index());
1802 size_t remote_content_size =
1803 current_remote_desc->description()->contents().size();
1804 if (mediacontent_index >= remote_content_size) {
1805 LOG(LS_ERROR)
1806 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
1807
1808 *valid = false;
1809 return false;
1810 }
1811
1812 cricket::ContentInfo content =
1813 current_remote_desc->description()->contents()[mediacontent_index];
1814 transport_proxy = GetTransportProxy(content.name);
1815
1816 return transport_proxy && transport_proxy->local_description_set() &&
1817 transport_proxy->remote_description_set();
1818}
1819
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001820// Walk through the ConnectionInfos to gather best connection usage
1821// for IPv4 and IPv6.
1822void WebRtcSession::ReportBestConnectionState(cricket::Transport* transport) {
1823 if (!metrics_observer_) {
1824 return;
1825 }
1826
1827 cricket::TransportStats stats;
1828 if (!transport->GetStats(&stats)) {
1829 return;
1830 }
1831
1832 for (cricket::TransportChannelStatsList::const_iterator it =
1833 stats.channel_stats.begin();
1834 it != stats.channel_stats.end(); ++it) {
1835 for (cricket::ConnectionInfos::const_iterator it_info =
1836 it->connection_infos.begin();
1837 it_info != it->connection_infos.end(); ++it_info) {
1838 if (!it_info->best_connection) {
1839 continue;
1840 }
1841 if (it_info->local_candidate.address().family() == AF_INET) {
1842 metrics_observer_->IncrementCounter(kBestConnections_IPv4);
1843 } else if (it_info->local_candidate.address().family() ==
1844 AF_INET6) {
1845 metrics_observer_->IncrementCounter(kBestConnections_IPv6);
1846 } else {
1847 ASSERT(false);
1848 }
1849 return;
1850 }
1851 }
1852}
1853
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854} // namespace webrtc