blob: 54018f223a5570fc86b5ba93998379cdcc11cacf [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: ";
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +000079const int kMaxUnsignalledRecvStreams = 20;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080
81// Compares |answer| against |offer|. Comparision is done
82// for number of m-lines in answer against offer. If matches true will be
83// returned otherwise false.
84static bool VerifyMediaDescriptions(
85 const SessionDescription* answer, const SessionDescription* offer) {
86 if (offer->contents().size() != answer->contents().size())
87 return false;
88
89 for (size_t i = 0; i < offer->contents().size(); ++i) {
90 if ((offer->contents()[i].name) != answer->contents()[i].name) {
91 return false;
92 }
wu@webrtc.org4e393072014-04-07 17:04:35 +000093 const MediaContentDescription* offer_mdesc =
94 static_cast<const MediaContentDescription*>(
95 offer->contents()[i].description);
96 const MediaContentDescription* answer_mdesc =
97 static_cast<const MediaContentDescription*>(
98 answer->contents()[i].description);
99 if (offer_mdesc->type() != answer_mdesc->type()) {
100 return false;
101 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 }
103 return true;
104}
105
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106// Checks that each non-rejected content has SDES crypto keys or a DTLS
107// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
108// keys, will be caught in Transport negotiation, and backstopped by Channel's
109// |secure_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000110static bool VerifyCrypto(const SessionDescription* desc,
111 bool dtls_enabled,
112 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 const ContentInfos& contents = desc->contents();
114 for (size_t index = 0; index < contents.size(); ++index) {
115 const ContentInfo* cinfo = &contents[index];
116 if (cinfo->rejected) {
117 continue;
118 }
119
120 // If the content isn't rejected, crypto must be present.
121 const MediaContentDescription* media =
122 static_cast<const MediaContentDescription*>(cinfo->description);
123 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
124 if (!media || !tinfo) {
125 // Something is not right.
126 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000127 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 return false;
129 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000130 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000131 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000132 LOG(LS_WARNING) <<
133 "Session description must have DTLS fingerprint if DTLS enabled.";
134 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000135 return false;
136 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000137 } else {
138 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000139 LOG(LS_WARNING) <<
140 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000141 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000142 return false;
143 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144 }
145 }
146
147 return true;
148}
149
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000150// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
151static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
152 const ContentInfos& contents = desc->contents();
153 for (size_t index = 0; index < contents.size(); ++index) {
154 const ContentInfo* cinfo = &contents[index];
155 if (cinfo->rejected) {
156 continue;
157 }
158
159 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
160 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
161 if (!tinfo) {
162 // Something is not right.
163 LOG(LS_ERROR) << kInvalidSdp;
164 return false;
165 }
166 if (tinfo->description.ice_ufrag.empty() ||
167 tinfo->description.ice_pwd.empty()) {
168 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
169 return false;
170 }
171 }
172 return true;
173}
174
wu@webrtc.org91053e72013-08-10 07:18:04 +0000175// Forces |sdesc->crypto_required| to the appropriate state based on the
176// current security policy, to ensure a failure occurs if there is an error
177// in crypto negotiation.
178// Called when processing the local session description.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000179static void UpdateSessionDescriptionSecurePolicy(cricket::CryptoType type,
180 SessionDescription* sdesc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000181 if (!sdesc) {
182 return;
183 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184
wu@webrtc.org91053e72013-08-10 07:18:04 +0000185 // Updating the |crypto_required_| in MediaContentDescription to the
186 // appropriate state based on the current security policy.
187 for (cricket::ContentInfos::iterator iter = sdesc->contents().begin();
188 iter != sdesc->contents().end(); ++iter) {
189 if (cricket::IsMediaContent(&*iter)) {
190 MediaContentDescription* mdesc =
191 static_cast<MediaContentDescription*> (iter->description);
192 if (mdesc) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000193 mdesc->set_crypto_required(type);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000194 }
195 }
196 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197}
198
199static bool GetAudioSsrcByTrackId(
200 const SessionDescription* session_description,
201 const std::string& track_id, uint32 *ssrc) {
202 const cricket::ContentInfo* audio_info =
203 cricket::GetFirstAudioContent(session_description);
204 if (!audio_info) {
205 LOG(LS_ERROR) << "Audio not used in this call";
206 return false;
207 }
208
209 const cricket::MediaContentDescription* audio_content =
210 static_cast<const cricket::MediaContentDescription*>(
211 audio_info->description);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000212 const cricket::StreamParams* stream =
213 cricket::GetStreamByIds(audio_content->streams(), "", track_id);
214 if (!stream) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215 return false;
216 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000217
218 *ssrc = stream->first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219 return true;
220}
221
222static bool GetTrackIdBySsrc(const SessionDescription* session_description,
223 uint32 ssrc, std::string* track_id) {
224 ASSERT(track_id != NULL);
225
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226 const cricket::ContentInfo* audio_info =
227 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000228 if (audio_info) {
229 const cricket::MediaContentDescription* audio_content =
230 static_cast<const cricket::MediaContentDescription*>(
231 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000233 const auto* found =
234 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
235 if (found) {
236 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000237 return true;
238 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239 }
240
241 const cricket::ContentInfo* video_info =
242 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000243 if (video_info) {
244 const cricket::MediaContentDescription* video_content =
245 static_cast<const cricket::MediaContentDescription*>(
246 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000248 const auto* found =
249 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
250 if (found) {
251 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000252 return true;
253 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254 }
255 return false;
256}
257
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000258static bool BadSdp(const std::string& source,
259 const std::string& type,
260 const std::string& reason,
261 std::string* err_desc) {
262 std::ostringstream desc;
263 desc << "Failed to set " << source << " " << type << " sdp: " << reason;
264
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000266 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000268 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269 return false;
270}
271
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000273 const std::string& type,
274 const std::string& reason,
275 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000277 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000279 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280 }
281}
282
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000283static bool BadLocalSdp(const std::string& type,
284 const std::string& reason,
285 std::string* err_desc) {
286 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
287}
288
289static bool BadRemoteSdp(const std::string& type,
290 const std::string& reason,
291 std::string* err_desc) {
292 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
293}
294
295static bool BadOfferSdp(cricket::ContentSource source,
296 const std::string& reason,
297 std::string* err_desc) {
298 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
299}
300
301static bool BadPranswerSdp(cricket::ContentSource source,
302 const std::string& reason,
303 std::string* err_desc) {
304 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
305 reason, err_desc);
306}
307
308static bool BadAnswerSdp(cricket::ContentSource source,
309 const std::string& reason,
310 std::string* err_desc) {
311 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312}
313
314#define GET_STRING_OF_STATE(state) \
315 case cricket::BaseSession::state: \
316 result = #state; \
317 break;
318
319static std::string GetStateString(cricket::BaseSession::State state) {
320 std::string result;
321 switch (state) {
322 GET_STRING_OF_STATE(STATE_INIT)
323 GET_STRING_OF_STATE(STATE_SENTINITIATE)
324 GET_STRING_OF_STATE(STATE_RECEIVEDINITIATE)
325 GET_STRING_OF_STATE(STATE_SENTPRACCEPT)
326 GET_STRING_OF_STATE(STATE_SENTACCEPT)
327 GET_STRING_OF_STATE(STATE_RECEIVEDPRACCEPT)
328 GET_STRING_OF_STATE(STATE_RECEIVEDACCEPT)
329 GET_STRING_OF_STATE(STATE_SENTMODIFY)
330 GET_STRING_OF_STATE(STATE_RECEIVEDMODIFY)
331 GET_STRING_OF_STATE(STATE_SENTREJECT)
332 GET_STRING_OF_STATE(STATE_RECEIVEDREJECT)
333 GET_STRING_OF_STATE(STATE_SENTREDIRECT)
334 GET_STRING_OF_STATE(STATE_SENTTERMINATE)
335 GET_STRING_OF_STATE(STATE_RECEIVEDTERMINATE)
336 GET_STRING_OF_STATE(STATE_INPROGRESS)
337 GET_STRING_OF_STATE(STATE_DEINIT)
338 default:
339 ASSERT(false);
340 break;
341 }
342 return result;
343}
344
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000345#define GET_STRING_OF_ERROR_CODE(err) \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346 case cricket::BaseSession::err: \
347 result = #err; \
348 break;
349
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000350static std::string GetErrorCodeString(cricket::BaseSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000351 std::string result;
352 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000353 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
354 GET_STRING_OF_ERROR_CODE(ERROR_TIME)
355 GET_STRING_OF_ERROR_CODE(ERROR_RESPONSE)
356 GET_STRING_OF_ERROR_CODE(ERROR_NETWORK)
357 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
358 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359 default:
360 ASSERT(false);
361 break;
362 }
363 return result;
364}
365
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000366static std::string MakeErrorString(const std::string& error,
367 const std::string& desc) {
368 std::ostringstream ret;
369 ret << error << " " << desc;
370 return ret.str();
371}
372
373static std::string MakeTdErrorString(const std::string& desc) {
374 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000375}
376
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000377// Set |option| to the highest-priority value of |key| in the optional
378// constraints if the key is found and has a valid value.
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000379template<typename T>
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000380static void SetOptionFromOptionalConstraint(
381 const MediaConstraintsInterface* constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000382 const std::string& key, cricket::Settable<T>* option) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000383 if (!constraints) {
384 return;
385 }
386 std::string string_value;
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000387 T value;
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000388 if (constraints->GetOptional().FindFirst(key, &string_value)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000389 if (rtc::FromString(string_value, &value)) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000390 option->Set(value);
391 }
392 }
393}
394
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000395uint32 ConvertIceTransportTypeToCandidateFilter(
396 PeerConnectionInterface::IceTransportsType type) {
397 switch (type) {
398 case PeerConnectionInterface::kNone:
399 return cricket::CF_NONE;
400 case PeerConnectionInterface::kRelay:
401 return cricket::CF_RELAY;
402 case PeerConnectionInterface::kNoHost:
403 return (cricket::CF_ALL & ~cricket::CF_HOST);
404 case PeerConnectionInterface::kAll:
405 return cricket::CF_ALL;
406 default: ASSERT(false);
407 }
408 return cricket::CF_NONE;
409}
410
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000411// Help class used to remember if a a remote peer has requested ice restart by
412// by sending a description with new ice ufrag and password.
413class IceRestartAnswerLatch {
414 public:
415 IceRestartAnswerLatch() : ice_restart_(false) { }
416
wu@webrtc.org91053e72013-08-10 07:18:04 +0000417 // Returns true if CheckForRemoteIceRestart has been called with a new session
418 // description where ice password and ufrag has changed since last time
419 // Reset() was called.
420 bool Get() const {
421 return ice_restart_;
422 }
423
424 void Reset() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000425 if (ice_restart_) {
426 ice_restart_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428 }
429
430 void CheckForRemoteIceRestart(
431 const SessionDescriptionInterface* old_desc,
432 const SessionDescriptionInterface* new_desc) {
433 if (!old_desc || new_desc->type() != SessionDescriptionInterface::kOffer) {
434 return;
435 }
436 const SessionDescription* new_sd = new_desc->description();
437 const SessionDescription* old_sd = old_desc->description();
438 const ContentInfos& contents = new_sd->contents();
439 for (size_t index = 0; index < contents.size(); ++index) {
440 const ContentInfo* cinfo = &contents[index];
441 if (cinfo->rejected) {
442 continue;
443 }
444 // If the content isn't rejected, check if ufrag and password has
445 // changed.
446 const cricket::TransportDescription* new_transport_desc =
447 new_sd->GetTransportDescriptionByName(cinfo->name);
448 const cricket::TransportDescription* old_transport_desc =
449 old_sd->GetTransportDescriptionByName(cinfo->name);
450 if (!new_transport_desc || !old_transport_desc) {
451 // No transport description exist. This is not an ice restart.
452 continue;
453 }
jiayl@webrtc.orgdb397e52014-06-20 16:32:09 +0000454 if (cricket::IceCredentialsChanged(old_transport_desc->ice_ufrag,
455 old_transport_desc->ice_pwd,
456 new_transport_desc->ice_ufrag,
457 new_transport_desc->ice_pwd)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458 LOG(LS_INFO) << "Remote peer request ice restart.";
459 ice_restart_ = true;
460 break;
461 }
462 }
463 }
464
465 private:
466 bool ice_restart_;
467};
468
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000469WebRtcSession::WebRtcSession(cricket::ChannelManager* channel_manager,
470 rtc::Thread* signaling_thread,
471 rtc::Thread* worker_thread,
472 cricket::PortAllocator* port_allocator,
473 MediaStreamSignaling* mediastream_signaling)
474 : cricket::BaseSession(signaling_thread,
475 worker_thread,
476 port_allocator,
477 rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX),
478 cricket::NS_JINGLE_RTP,
479 false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480 // RFC 3264: The numeric value of the session id and version in the
481 // o line MUST be representable with a "64 bit signed integer".
482 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
483 channel_manager_(channel_manager),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484 mediastream_signaling_(mediastream_signaling),
485 ice_observer_(NULL),
486 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000488 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000490 ice_restart_latch_(new IceRestartAnswerLatch),
491 metrics_observer_(NULL) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492}
493
494WebRtcSession::~WebRtcSession() {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000495 // Destroy video_channel_ first since it may have a pointer to the
496 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 if (video_channel_.get()) {
498 SignalVideoChannelDestroyed();
499 channel_manager_->DestroyVideoChannel(video_channel_.release());
500 }
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000501 if (voice_channel_.get()) {
502 SignalVoiceChannelDestroyed();
503 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
504 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505 if (data_channel_.get()) {
506 SignalDataChannelDestroyed();
507 channel_manager_->DestroyDataChannel(data_channel_.release());
508 }
509 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
510 delete saved_candidates_[i];
511 }
512 delete identity();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513}
514
wu@webrtc.org91053e72013-08-10 07:18:04 +0000515bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000516 const PeerConnectionFactoryInterface::Options& options,
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000517 const MediaConstraintsInterface* constraints,
518 DTLSIdentityServiceInterface* dtls_identity_service,
519 PeerConnectionInterface::IceTransportsType ice_transport) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520 // TODO(perkj): Take |constraints| into consideration. Return false if not all
521 // mandatory constraints can be fulfilled. Note that |constraints|
522 // can be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523 bool value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000524
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000525 if (options.disable_encryption) {
526 dtls_enabled_ = false;
527 } else {
528 // Enable DTLS by default if |dtls_identity_service| is valid.
529 dtls_enabled_ = (dtls_identity_service != NULL);
530 // |constraints| can override the default |dtls_enabled_| value.
531 if (FindConstraint(
532 constraints,
533 MediaConstraintsInterface::kEnableDtlsSrtp,
534 &value, NULL)) {
535 dtls_enabled_ = value;
536 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000537 }
538
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000540 // It takes precendence over the disable_sctp_data_channels
541 // PeerConnectionFactoryInterface::Options.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542 if (FindConstraint(
543 constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
544 &value, NULL) && value) {
545 LOG(LS_INFO) << "Allowing RTP data engine.";
546 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000547 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000548 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000549 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000550 LOG(LS_INFO) << "Allowing SCTP data engine.";
551 data_channel_type_ = cricket::DCT_SCTP;
552 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 }
554 if (data_channel_type_ != cricket::DCT_NONE) {
555 mediastream_signaling_->SetDataChannelFactory(this);
556 }
557
wu@webrtc.orgde305012013-10-31 15:40:38 +0000558 // Find DSCP constraint.
559 if (FindConstraint(
560 constraints,
561 MediaConstraintsInterface::kEnableDscp,
562 &value, NULL)) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000563 audio_options_.dscp.Set(value);
564 video_options_.dscp.Set(value);
565 }
566
567 // Find Suspend Below Min Bitrate constraint.
568 if (FindConstraint(
569 constraints,
570 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
571 &value,
572 NULL)) {
573 video_options_.suspend_below_min_bitrate.Set(value);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000574 }
575
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000576 SetOptionFromOptionalConstraint(constraints,
577 MediaConstraintsInterface::kScreencastMinBitrate,
578 &video_options_.screencast_min_bitrate);
579
580 // Find constraints for cpu overuse detection.
581 SetOptionFromOptionalConstraint(constraints,
582 MediaConstraintsInterface::kCpuUnderuseThreshold,
583 &video_options_.cpu_underuse_threshold);
584 SetOptionFromOptionalConstraint(constraints,
585 MediaConstraintsInterface::kCpuOveruseThreshold,
586 &video_options_.cpu_overuse_threshold);
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000587 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000588 MediaConstraintsInterface::kCpuOveruseDetection,
589 &video_options_.cpu_overuse_detection);
590 SetOptionFromOptionalConstraint(constraints,
591 MediaConstraintsInterface::kCpuOveruseEncodeUsage,
592 &video_options_.cpu_overuse_encode_usage);
593 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000594 MediaConstraintsInterface::kCpuUnderuseEncodeRsdThreshold,
595 &video_options_.cpu_underuse_encode_rsd_threshold);
596 SetOptionFromOptionalConstraint(constraints,
597 MediaConstraintsInterface::kCpuOveruseEncodeRsdThreshold,
598 &video_options_.cpu_overuse_encode_rsd_threshold);
buildbot@webrtc.orgdb563902014-06-13 13:05:48 +0000599
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000600 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000601 MediaConstraintsInterface::kNumUnsignalledRecvStreams,
602 &video_options_.unsignalled_recv_stream_limit);
603 if (video_options_.unsignalled_recv_stream_limit.IsSet()) {
604 int stream_limit;
605 video_options_.unsignalled_recv_stream_limit.Get(&stream_limit);
606 stream_limit = rtc::_min(kMaxUnsignalledRecvStreams, stream_limit);
607 stream_limit = rtc::_max(0, stream_limit);
608 video_options_.unsignalled_recv_stream_limit.Set(stream_limit);
609 }
610
611 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000612 MediaConstraintsInterface::kHighStartBitrate,
613 &video_options_.video_start_bitrate);
wu@webrtc.orgcfe5e9c2014-03-27 17:03:58 +0000614
615 if (FindConstraint(
616 constraints,
617 MediaConstraintsInterface::kVeryHighBitrate,
618 &value,
619 NULL)) {
620 video_options_.video_highest_bitrate.Set(
621 cricket::VideoOptions::VERY_HIGH);
622 } else if (FindConstraint(
623 constraints,
624 MediaConstraintsInterface::kHighBitrate,
625 &value,
626 NULL)) {
627 video_options_.video_highest_bitrate.Set(
628 cricket::VideoOptions::HIGH);
629 }
630
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000631 SetOptionFromOptionalConstraint(constraints,
632 MediaConstraintsInterface::kCombinedAudioVideoBwe,
633 &audio_options_.combined_audio_video_bwe);
634
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635 const cricket::VideoCodec default_codec(
636 JsepSessionDescription::kDefaultVideoCodecId,
637 JsepSessionDescription::kDefaultVideoCodecName,
638 JsepSessionDescription::kMaxVideoCodecWidth,
639 JsepSessionDescription::kMaxVideoCodecHeight,
640 JsepSessionDescription::kDefaultVideoCodecFramerate,
641 JsepSessionDescription::kDefaultVideoCodecPreference);
642 channel_manager_->SetDefaultVideoEncoderConfig(
643 cricket::VideoEncoderConfig(default_codec));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000644
645 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
646 signaling_thread(),
647 channel_manager_,
648 mediastream_signaling_,
649 dtls_identity_service,
650 this,
651 id(),
652 data_channel_type_,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000653 dtls_enabled_));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000654
655 webrtc_session_desc_factory_->SignalIdentityReady.connect(
656 this, &WebRtcSession::OnIdentityReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000657
wu@webrtc.org97077a32013-10-25 21:18:33 +0000658 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000659 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000660 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000661 port_allocator()->set_candidate_filter(
662 ConvertIceTransportTypeToCandidateFilter(ice_transport));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000663 return true;
664}
665
666void WebRtcSession::Terminate() {
667 SetState(STATE_RECEIVEDTERMINATE);
668 RemoveUnusedChannelsAndTransports(NULL);
669 ASSERT(voice_channel_.get() == NULL);
670 ASSERT(video_channel_.get() == NULL);
671 ASSERT(data_channel_.get() == NULL);
672}
673
674bool WebRtcSession::StartCandidatesAllocation() {
675 // SpeculativelyConnectTransportChannels, will call ConnectChannels method
676 // from TransportProxy to start gathering ice candidates.
677 SpeculativelyConnectAllTransportChannels();
678 if (!saved_candidates_.empty()) {
679 // If there are saved candidates which arrived before local description is
680 // set, copy those to remote description.
681 CopySavedCandidates(remote_desc_.get());
682 }
683 // Push remote candidates present in remote description to transport channels.
684 UseCandidatesInSessionDescription(remote_desc_.get());
685 return true;
686}
687
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000688void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
689 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000690}
691
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000692cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
693 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694}
695
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000696bool WebRtcSession::GetSslRole(rtc::SSLRole* role) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000697 if (local_description() == NULL || remote_description() == NULL) {
698 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
699 << "SSL Role of the session.";
700 return false;
701 }
702
703 // TODO(mallinath) - Return role of each transport, as role may differ from
704 // one another.
705 // In current implementaion we just return the role of first transport in the
706 // transport map.
707 for (cricket::TransportMap::const_iterator iter = transport_proxies().begin();
708 iter != transport_proxies().end(); ++iter) {
709 if (iter->second->impl()) {
710 return iter->second->impl()->GetSslRole(role);
711 }
712 }
713 return false;
714}
715
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000716void WebRtcSession::CreateOffer(
717 CreateSessionDescriptionObserver* observer,
718 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
719 webrtc_session_desc_factory_->CreateOffer(observer, options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000720}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721
wu@webrtc.org91053e72013-08-10 07:18:04 +0000722void WebRtcSession::CreateAnswer(CreateSessionDescriptionObserver* observer,
723 const MediaConstraintsInterface* constraints) {
724 webrtc_session_desc_factory_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725}
726
727bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
728 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000729 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000730 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000731
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000732 // Validate SDP.
733 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
734 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 }
736
737 // Update the initiator flag if this session is the initiator.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000738 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000739 if (state() == STATE_INIT && action == kOffer) {
740 set_initiator(true);
741 }
742
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000743 cricket::SecurePolicy sdes_policy =
744 webrtc_session_desc_factory_->SdesPolicy();
745 cricket::CryptoType crypto_required = dtls_enabled_ ?
746 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
747 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000748 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000749 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750
751 set_local_description(desc->description()->Copy());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000752 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753
754 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000755 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756 // TODO(mallinath) - Handle CreateChannel failure, as new local description
757 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000758 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759 }
760
761 // Remove channel and transport proxies, if MediaContentDescription is
762 // rejected.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000763 RemoveUnusedChannelsAndTransports(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000765 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 return false;
767 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000768
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769 // Kick starting the ice candidates allocation.
770 StartCandidatesAllocation();
771
772 // Update state and SSRC of local MediaStreams and DataChannels based on the
773 // local session description.
774 mediastream_signaling_->OnLocalDescriptionChanged(local_desc_.get());
775
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000776 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000777 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
778 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
779 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000781 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782 }
783 return true;
784}
785
786bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
787 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000788 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000789 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000790
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000791 // Validate SDP.
792 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
793 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794 }
795
796 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000797 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 if (action == kOffer && !CreateChannels(desc->description())) {
799 // TODO(mallinath) - Handle CreateChannel failure, as new local description
800 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000801 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802 }
803
804 // Remove channel and transport proxies, if MediaContentDescription is
805 // rejected.
806 RemoveUnusedChannelsAndTransports(desc->description());
807
808 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
809 // is called.
810 set_remote_description(desc->description()->Copy());
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000811 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000812 return false;
813 }
814
815 // Update remote MediaStreams.
816 mediastream_signaling_->OnRemoteDescriptionChanged(desc);
817 if (local_description() && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000818 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819 }
820
821 // Copy all saved candidates.
822 CopySavedCandidates(desc);
823 // We retain all received candidates.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000824 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
825 remote_desc_.get(), desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000826 // Check if this new SessionDescription contains new ice ufrag and password
827 // that indicates the remote peer requests ice restart.
828 ice_restart_latch_->CheckForRemoteIceRestart(remote_desc_.get(),
829 desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000830 remote_desc_.reset(desc_temp.release());
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000831
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000832 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000833 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
834 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
835 }
836
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000838 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 }
840 return true;
841}
842
843bool WebRtcSession::UpdateSessionState(
844 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000845 std::string* err_desc) {
846 // If there's already a pending error then no state transition should happen.
847 // But all call-sites should be verifying this before calling us!
848 ASSERT(error() == cricket::BaseSession::ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000849 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000850 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000851 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
852 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 }
854 SetState(source == cricket::CS_LOCAL ?
855 STATE_SENTINITIATE : STATE_RECEIVEDINITIATE);
856 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000857 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 }
859 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000860 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
861 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 }
863 EnableChannels();
864 SetState(source == cricket::CS_LOCAL ?
865 STATE_SENTPRACCEPT : STATE_RECEIVEDPRACCEPT);
866 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000867 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868 }
869 } else if (action == kAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000870 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
871 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872 }
873 MaybeEnableMuxingSupport();
874 EnableChannels();
875 SetState(source == cricket::CS_LOCAL ?
876 STATE_SENTACCEPT : STATE_RECEIVEDACCEPT);
877 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000878 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879 }
880 }
881 return true;
882}
883
884WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
885 if (type == SessionDescriptionInterface::kOffer) {
886 return WebRtcSession::kOffer;
887 } else if (type == SessionDescriptionInterface::kPrAnswer) {
888 return WebRtcSession::kPrAnswer;
889 } else if (type == SessionDescriptionInterface::kAnswer) {
890 return WebRtcSession::kAnswer;
891 }
892 ASSERT(false && "unknown action type");
893 return WebRtcSession::kOffer;
894}
895
896bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
897 if (state() == STATE_INIT) {
898 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
899 << "without any offer (local or remote) "
900 << "session description.";
901 return false;
902 }
903
904 if (!candidate) {
905 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL";
906 return false;
907 }
908
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +0000909 bool valid = false;
910 if (!ReadyToUseRemoteCandidate(candidate, NULL, &valid)) {
911 if (valid) {
912 LOG(LS_INFO) << "ProcessIceMessage: Candidate saved";
913 saved_candidates_.push_back(
914 new JsepIceCandidate(candidate->sdp_mid(),
915 candidate->sdp_mline_index(),
916 candidate->candidate()));
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000917 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +0000918 return valid;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 }
920
921 // Add this candidate to the remote session description.
922 if (!remote_desc_->AddCandidate(candidate)) {
923 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used";
924 return false;
925 }
926
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000927 return UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928}
929
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000930bool WebRtcSession::SetIceTransports(
931 PeerConnectionInterface::IceTransportsType type) {
932 return port_allocator()->set_candidate_filter(
933 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000934}
935
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000936bool WebRtcSession::GetLocalTrackIdBySsrc(uint32 ssrc, std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000937 if (!BaseSession::local_description())
938 return false;
939 return webrtc::GetTrackIdBySsrc(
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000940 BaseSession::local_description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000941}
942
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000943bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32 ssrc, std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944 if (!BaseSession::remote_description())
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000945 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000946 return webrtc::GetTrackIdBySsrc(
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000947 BaseSession::remote_description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000948}
949
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000950std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000951 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000952 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000953 return desc.str();
954}
955
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000956void WebRtcSession::SetAudioPlayout(uint32 ssrc, bool enable,
957 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000958 ASSERT(signaling_thread()->IsCurrent());
959 if (!voice_channel_) {
960 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
961 return;
962 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000963 if (!voice_channel_->SetRemoteRenderer(ssrc, renderer)) {
964 // SetRenderer() can fail if the ssrc does not match any playout channel.
965 LOG(LS_ERROR) << "SetAudioPlayout: ssrc is incorrect: " << ssrc;
966 return;
967 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000968 if (!voice_channel_->SetOutputScaling(ssrc, enable ? 1 : 0, enable ? 1 : 0)) {
969 // Allow that SetOutputScaling fail if |enable| is false but assert
970 // otherwise. This in the normal case when the underlying media channel has
971 // already been deleted.
972 ASSERT(enable == false);
973 }
974}
975
976void WebRtcSession::SetAudioSend(uint32 ssrc, bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000977 const cricket::AudioOptions& options,
978 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000979 ASSERT(signaling_thread()->IsCurrent());
980 if (!voice_channel_) {
981 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
982 return;
983 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000984 if (!voice_channel_->SetLocalRenderer(ssrc, renderer)) {
985 // SetRenderer() can fail if the ssrc does not match any send channel.
986 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
987 return;
988 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989 if (!voice_channel_->MuteStream(ssrc, !enable)) {
990 // Allow that MuteStream fail if |enable| is false but assert otherwise.
991 // This in the normal case when the underlying media channel has already
992 // been deleted.
993 ASSERT(enable == false);
994 return;
995 }
996 if (enable)
997 voice_channel_->SetChannelOptions(options);
998}
999
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001000void WebRtcSession::SetAudioPlayoutVolume(uint32 ssrc, double volume) {
1001 ASSERT(signaling_thread()->IsCurrent());
1002 ASSERT(volume >= 0 && volume <= 10);
1003 if (!voice_channel_) {
1004 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1005 return;
1006 }
1007
1008 if (!voice_channel_->SetOutputScaling(ssrc, volume, volume))
1009 ASSERT(false);
1010}
1011
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001012bool WebRtcSession::SetCaptureDevice(uint32 ssrc,
1013 cricket::VideoCapturer* camera) {
1014 ASSERT(signaling_thread()->IsCurrent());
1015
1016 if (!video_channel_.get()) {
1017 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1018 // support video.
1019 LOG(LS_WARNING) << "Video not used in this call.";
1020 return false;
1021 }
1022 if (!video_channel_->SetCapturer(ssrc, camera)) {
1023 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1024 // This in the normal case when the underlying media channel has already
1025 // been deleted.
1026 ASSERT(camera == NULL);
1027 return false;
1028 }
1029 return true;
1030}
1031
1032void WebRtcSession::SetVideoPlayout(uint32 ssrc,
1033 bool enable,
1034 cricket::VideoRenderer* renderer) {
1035 ASSERT(signaling_thread()->IsCurrent());
1036 if (!video_channel_) {
1037 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1038 return;
1039 }
1040 if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) {
1041 // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise.
1042 // This in the normal case when the underlying media channel has already
1043 // been deleted.
1044 ASSERT(renderer == NULL);
1045 }
1046}
1047
1048void WebRtcSession::SetVideoSend(uint32 ssrc, bool enable,
1049 const cricket::VideoOptions* options) {
1050 ASSERT(signaling_thread()->IsCurrent());
1051 if (!video_channel_) {
1052 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1053 return;
1054 }
1055 if (!video_channel_->MuteStream(ssrc, !enable)) {
1056 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1057 // This in the normal case when the underlying media channel has already
1058 // been deleted.
1059 ASSERT(enable == false);
1060 return;
1061 }
1062 if (enable && options)
1063 video_channel_->SetChannelOptions(*options);
1064}
1065
1066bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1067 ASSERT(signaling_thread()->IsCurrent());
1068 if (!voice_channel_) {
1069 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1070 return false;
1071 }
1072 uint32 send_ssrc = 0;
1073 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1074 // exists.
1075 if (!GetAudioSsrcByTrackId(BaseSession::local_description(), track_id,
1076 &send_ssrc)) {
1077 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1078 return false;
1079 }
1080 return voice_channel_->CanInsertDtmf();
1081}
1082
1083bool WebRtcSession::InsertDtmf(const std::string& track_id,
1084 int code, int duration) {
1085 ASSERT(signaling_thread()->IsCurrent());
1086 if (!voice_channel_) {
1087 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1088 return false;
1089 }
1090 uint32 send_ssrc = 0;
1091 if (!VERIFY(GetAudioSsrcByTrackId(BaseSession::local_description(),
1092 track_id, &send_ssrc))) {
1093 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1094 return false;
1095 }
1096 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration,
1097 cricket::DF_SEND)) {
1098 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1099 return false;
1100 }
1101 return true;
1102}
1103
1104sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
1105 return &SignalVoiceChannelDestroyed;
1106}
1107
wu@webrtc.org78187522013-10-07 23:32:02 +00001108bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001109 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001110 cricket::SendDataResult* result) {
1111 if (!data_channel_.get()) {
1112 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1113 return false;
1114 }
1115 return data_channel_->SendData(params, payload, result);
1116}
1117
1118bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
1119 if (!data_channel_.get()) {
1120 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1121 return false;
1122 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001123 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1124 &DataChannel::OnChannelReady);
1125 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1126 &DataChannel::OnDataReceived);
wu@webrtc.org78187522013-10-07 23:32:02 +00001127 return true;
1128}
1129
1130void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001131 if (!data_channel_.get()) {
1132 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1133 return;
1134 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001135 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1136 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
1137}
1138
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001139void WebRtcSession::AddSctpDataStream(int sid) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001140 if (!data_channel_.get()) {
1141 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1142 return;
1143 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001144 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1145 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001146}
1147
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001148void WebRtcSession::RemoveSctpDataStream(int sid) {
1149 mediastream_signaling_->RemoveSctpDataChannel(sid);
jiayl@webrtc.org2eaac182014-06-17 16:02:46 +00001150
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001151 if (!data_channel_.get()) {
1152 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1153 << "NULL.";
1154 return;
1155 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001156 data_channel_->RemoveRecvStream(sid);
1157 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001158}
1159
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001160bool WebRtcSession::ReadyToSendData() const {
1161 return data_channel_.get() && data_channel_->ready_to_send_data();
1162}
1163
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001164rtc::scoped_refptr<DataChannel> WebRtcSession::CreateDataChannel(
wu@webrtc.org78187522013-10-07 23:32:02 +00001165 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001166 const InternalDataChannelInit* config) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001167 if (state() == STATE_RECEIVEDTERMINATE) {
1168 return NULL;
1169 }
1170 if (data_channel_type_ == cricket::DCT_NONE) {
1171 LOG(LS_ERROR) << "CreateDataChannel: Data is not supported in this call.";
1172 return NULL;
1173 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001174 InternalDataChannelInit new_config =
1175 config ? (*config) : InternalDataChannelInit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001176 if (data_channel_type_ == cricket::DCT_SCTP) {
1177 if (new_config.id < 0) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001178 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001179 if (GetSslRole(&role) &&
1180 !mediastream_signaling_->AllocateSctpSid(role, &new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181 LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel.";
1182 return NULL;
1183 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001184 } else if (!mediastream_signaling_->IsSctpSidAvailable(new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001185 LOG(LS_ERROR) << "Failed to create a SCTP data channel "
1186 << "because the id is already in use or out of range.";
1187 return NULL;
1188 }
1189 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001190
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001191 rtc::scoped_refptr<DataChannel> channel(DataChannel::Create(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001192 this, data_channel_type_, label, new_config));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001193 if (channel && !mediastream_signaling_->AddDataChannel(channel))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001194 return NULL;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001195
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001196 return channel;
1197}
1198
1199cricket::DataChannelType WebRtcSession::data_channel_type() const {
1200 return data_channel_type_;
1201}
1202
wu@webrtc.org91053e72013-08-10 07:18:04 +00001203bool WebRtcSession::IceRestartPending() const {
1204 return ice_restart_latch_->Get();
1205}
1206
1207void WebRtcSession::ResetIceRestartLatch() {
1208 ice_restart_latch_->Reset();
1209}
1210
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001211void WebRtcSession::OnIdentityReady(rtc::SSLIdentity* identity) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001212 SetIdentity(identity);
1213}
1214
1215bool WebRtcSession::waiting_for_identity() const {
1216 return webrtc_session_desc_factory_->waiting_for_identity();
1217}
1218
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001219void WebRtcSession::SetIceConnectionState(
1220 PeerConnectionInterface::IceConnectionState state) {
1221 if (ice_connection_state_ == state) {
1222 return;
1223 }
1224
1225 // ASSERT that the requested transition is allowed. Note that
1226 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1227 // within PeerConnection). This switch statement should compile away when
1228 // ASSERTs are disabled.
1229 switch (ice_connection_state_) {
1230 case PeerConnectionInterface::kIceConnectionNew:
1231 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1232 break;
1233 case PeerConnectionInterface::kIceConnectionChecking:
1234 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1235 state == PeerConnectionInterface::kIceConnectionConnected);
1236 break;
1237 case PeerConnectionInterface::kIceConnectionConnected:
1238 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1239 state == PeerConnectionInterface::kIceConnectionChecking ||
1240 state == PeerConnectionInterface::kIceConnectionCompleted);
1241 break;
1242 case PeerConnectionInterface::kIceConnectionCompleted:
1243 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1244 state == PeerConnectionInterface::kIceConnectionDisconnected);
1245 break;
1246 case PeerConnectionInterface::kIceConnectionFailed:
1247 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1248 break;
1249 case PeerConnectionInterface::kIceConnectionDisconnected:
1250 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1251 state == PeerConnectionInterface::kIceConnectionConnected ||
1252 state == PeerConnectionInterface::kIceConnectionCompleted ||
1253 state == PeerConnectionInterface::kIceConnectionFailed);
1254 break;
1255 case PeerConnectionInterface::kIceConnectionClosed:
1256 ASSERT(false);
1257 break;
1258 default:
1259 ASSERT(false);
1260 break;
1261 }
1262
1263 ice_connection_state_ = state;
1264 if (ice_observer_) {
1265 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1266 }
1267}
1268
1269void WebRtcSession::OnTransportRequestSignaling(
1270 cricket::Transport* transport) {
1271 ASSERT(signaling_thread()->IsCurrent());
1272 transport->OnSignalingReady();
1273 if (ice_observer_) {
1274 ice_observer_->OnIceGatheringChange(
1275 PeerConnectionInterface::kIceGatheringGathering);
1276 }
1277}
1278
1279void WebRtcSession::OnTransportConnecting(cricket::Transport* transport) {
1280 ASSERT(signaling_thread()->IsCurrent());
1281 // start monitoring for the write state of the transport.
1282 OnTransportWritable(transport);
1283}
1284
1285void WebRtcSession::OnTransportWritable(cricket::Transport* transport) {
1286 ASSERT(signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001287 if (transport->all_channels_writable()) {
henrike@webrtc.org05376342014-03-10 15:53:12 +00001288 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001289 } else if (transport->HasChannels()) {
1290 // If the current state is Connected or Completed, then there were writable
1291 // channels but now there are not, so the next state must be Disconnected.
1292 if (ice_connection_state_ ==
1293 PeerConnectionInterface::kIceConnectionConnected ||
1294 ice_connection_state_ ==
1295 PeerConnectionInterface::kIceConnectionCompleted) {
1296 SetIceConnectionState(
1297 PeerConnectionInterface::kIceConnectionDisconnected);
1298 }
1299 }
1300}
1301
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001302void WebRtcSession::OnTransportCompleted(cricket::Transport* transport) {
1303 ASSERT(signaling_thread()->IsCurrent());
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001304 PeerConnectionInterface::IceConnectionState old_state = ice_connection_state_;
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001305 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001306 // Only report once when Ice connection is completed.
1307 if (old_state != PeerConnectionInterface::kIceConnectionCompleted) {
1308 ReportBestConnectionState(transport);
1309 }
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001310}
1311
1312void WebRtcSession::OnTransportFailed(cricket::Transport* transport) {
1313 ASSERT(signaling_thread()->IsCurrent());
1314 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1315}
1316
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001317void WebRtcSession::OnTransportProxyCandidatesReady(
1318 cricket::TransportProxy* proxy, const cricket::Candidates& candidates) {
1319 ASSERT(signaling_thread()->IsCurrent());
1320 ProcessNewLocalCandidate(proxy->content_name(), candidates);
1321}
1322
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001323void WebRtcSession::OnCandidatesAllocationDone() {
1324 ASSERT(signaling_thread()->IsCurrent());
1325 if (ice_observer_) {
1326 ice_observer_->OnIceGatheringChange(
1327 PeerConnectionInterface::kIceGatheringComplete);
1328 ice_observer_->OnIceComplete();
1329 }
1330}
1331
1332// Enabling voice and video channel.
1333void WebRtcSession::EnableChannels() {
1334 if (voice_channel_ && !voice_channel_->enabled())
1335 voice_channel_->Enable(true);
1336
1337 if (video_channel_ && !video_channel_->enabled())
1338 video_channel_->Enable(true);
1339
1340 if (data_channel_.get() && !data_channel_->enabled())
1341 data_channel_->Enable(true);
1342}
1343
1344void WebRtcSession::ProcessNewLocalCandidate(
1345 const std::string& content_name,
1346 const cricket::Candidates& candidates) {
1347 int sdp_mline_index;
1348 if (!GetLocalCandidateMediaIndex(content_name, &sdp_mline_index)) {
1349 LOG(LS_ERROR) << "ProcessNewLocalCandidate: content name "
1350 << content_name << " not found";
1351 return;
1352 }
1353
1354 for (cricket::Candidates::const_iterator citer = candidates.begin();
1355 citer != candidates.end(); ++citer) {
1356 // Use content_name as the candidate media id.
1357 JsepIceCandidate candidate(content_name, sdp_mline_index, *citer);
1358 if (ice_observer_) {
1359 ice_observer_->OnIceCandidate(&candidate);
1360 }
1361 if (local_desc_) {
1362 local_desc_->AddCandidate(&candidate);
1363 }
1364 }
1365}
1366
1367// Returns the media index for a local ice candidate given the content name.
1368bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1369 int* sdp_mline_index) {
1370 if (!BaseSession::local_description() || !sdp_mline_index)
1371 return false;
1372
1373 bool content_found = false;
1374 const ContentInfos& contents = BaseSession::local_description()->contents();
1375 for (size_t index = 0; index < contents.size(); ++index) {
1376 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001377 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001378 content_found = true;
1379 break;
1380 }
1381 }
1382 return content_found;
1383}
1384
1385bool WebRtcSession::UseCandidatesInSessionDescription(
1386 const SessionDescriptionInterface* remote_desc) {
1387 if (!remote_desc)
1388 return true;
1389 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001390
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001391 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1392 const IceCandidateCollection* candidates = remote_desc->candidates(m);
1393 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001394 const IceCandidateInterface* candidate = candidates->at(n);
1395 bool valid = false;
1396 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1397 if (valid) {
1398 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Candidate saved.";
1399 saved_candidates_.push_back(
1400 new JsepIceCandidate(candidate->sdp_mid(),
1401 candidate->sdp_mline_index(),
1402 candidate->candidate()));
1403 }
1404 continue;
1405 }
1406
1407 ret = UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001408 if (!ret)
1409 break;
1410 }
1411 }
1412 return ret;
1413}
1414
1415bool WebRtcSession::UseCandidate(
1416 const IceCandidateInterface* candidate) {
1417
1418 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
1419 size_t remote_content_size =
1420 BaseSession::remote_description()->contents().size();
1421 if (mediacontent_index >= remote_content_size) {
1422 LOG(LS_ERROR)
1423 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1424 return false;
1425 }
1426
1427 cricket::ContentInfo content =
1428 BaseSession::remote_description()->contents()[mediacontent_index];
1429 std::vector<cricket::Candidate> candidates;
1430 candidates.push_back(candidate->candidate());
1431 // Invoking BaseSession method to handle remote candidates.
1432 std::string error;
1433 if (OnRemoteCandidates(content.name, candidates, &error)) {
1434 // Candidates successfully submitted for checking.
1435 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1436 ice_connection_state_ ==
1437 PeerConnectionInterface::kIceConnectionDisconnected) {
1438 // If state is New, then the session has just gotten its first remote ICE
1439 // candidates, so go to Checking.
1440 // If state is Disconnected, the session is re-using old candidates or
1441 // receiving additional ones, so go to Checking.
1442 // If state is Connected, stay Connected.
1443 // TODO(bemasc): If state is Connected, and the new candidates are for a
1444 // newly added transport, then the state actually _should_ move to
1445 // checking. Add a way to distinguish that case.
1446 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1447 }
1448 // TODO(bemasc): If state is Completed, go back to Connected.
1449 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001450 if (!error.empty()) {
1451 LOG(LS_WARNING) << error;
1452 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001453 }
1454 return true;
1455}
1456
1457void WebRtcSession::RemoveUnusedChannelsAndTransports(
1458 const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001459 // Destroy video_channel_ first since it may have a pointer to the
1460 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001461 const cricket::ContentInfo* video_info =
1462 cricket::GetFirstVideoContent(desc);
1463 if ((!video_info || video_info->rejected) && video_channel_) {
1464 mediastream_signaling_->OnVideoChannelClose();
1465 SignalVideoChannelDestroyed();
1466 const std::string content_name = video_channel_->content_name();
1467 channel_manager_->DestroyVideoChannel(video_channel_.release());
1468 DestroyTransportProxy(content_name);
1469 }
1470
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001471 const cricket::ContentInfo* voice_info =
1472 cricket::GetFirstAudioContent(desc);
1473 if ((!voice_info || voice_info->rejected) && voice_channel_) {
1474 mediastream_signaling_->OnAudioChannelClose();
1475 SignalVoiceChannelDestroyed();
1476 const std::string content_name = voice_channel_->content_name();
1477 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
1478 DestroyTransportProxy(content_name);
1479 }
1480
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001481 const cricket::ContentInfo* data_info =
1482 cricket::GetFirstDataContent(desc);
1483 if ((!data_info || data_info->rejected) && data_channel_) {
1484 mediastream_signaling_->OnDataChannelClose();
1485 SignalDataChannelDestroyed();
1486 const std::string content_name = data_channel_->content_name();
1487 channel_manager_->DestroyDataChannel(data_channel_.release());
1488 DestroyTransportProxy(content_name);
1489 }
1490}
1491
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001492// TODO(mallinath) - Add a correct error code if the channels are not creatued
1493// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001494bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
1495 // Disabling the BUNDLE flag in PortAllocator if offer disabled it.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001496 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1497 if (state() == STATE_INIT && !bundle_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001498 port_allocator()->set_flags(port_allocator()->flags() &
1499 ~cricket::PORTALLOCATOR_ENABLE_BUNDLE);
1500 }
1501
1502 // Creating the media channels and transport proxies.
1503 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1504 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001505 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001506 LOG(LS_ERROR) << "Failed to create voice channel.";
1507 return false;
1508 }
1509 }
1510
1511 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1512 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001513 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001514 LOG(LS_ERROR) << "Failed to create video channel.";
1515 return false;
1516 }
1517 }
1518
1519 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1520 if (data_channel_type_ != cricket::DCT_NONE &&
1521 data && !data->rejected && !data_channel_.get()) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001522 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001523 LOG(LS_ERROR) << "Failed to create data channel.";
1524 return false;
1525 }
1526 }
1527
1528 return true;
1529}
1530
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001531bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001532 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001533 this, content->name, true));
wu@webrtc.orgde305012013-10-31 15:40:38 +00001534 if (!voice_channel_.get())
1535 return false;
1536
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00001537 voice_channel_->SetChannelOptions(audio_options_);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001538 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001539}
1540
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001541bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001542 video_channel_.reset(channel_manager_->CreateVideoChannel(
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +00001543 this, content->name, true, video_options_, voice_channel_.get()));
1544 return video_channel_.get() != NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001545}
1546
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001547bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001548 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001549 data_channel_.reset(channel_manager_->CreateDataChannel(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001550 this, content->name, !sctp, data_channel_type_));
wu@webrtc.org91053e72013-08-10 07:18:04 +00001551 if (!data_channel_.get()) {
1552 return false;
1553 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001554 if (sctp) {
1555 mediastream_signaling_->OnDataTransportCreatedForSctp();
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001556 data_channel_->SignalDataReceived.connect(
1557 this, &WebRtcSession::OnDataChannelMessageReceived);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00001558 data_channel_->SignalStreamClosedRemotely.connect(
1559 mediastream_signaling_,
1560 &MediaStreamSignaling::OnRemoteSctpDataChannelClosed);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001561 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001562 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001563}
1564
1565void WebRtcSession::CopySavedCandidates(
1566 SessionDescriptionInterface* dest_desc) {
1567 if (!dest_desc) {
1568 ASSERT(false);
1569 return;
1570 }
1571 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
1572 dest_desc->AddCandidate(saved_candidates_[i]);
1573 delete saved_candidates_[i];
1574 }
1575 saved_candidates_.clear();
1576}
1577
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001578void WebRtcSession::OnDataChannelMessageReceived(
1579 cricket::DataChannel* channel,
1580 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001581 const rtc::Buffer& payload) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001582 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001583 if (params.type == cricket::DMT_CONTROL &&
1584 mediastream_signaling_->IsSctpSidAvailable(params.ssrc)) {
1585 // Received CONTROL on unused sid, process as an OPEN message.
1586 mediastream_signaling_->AddDataChannelFromOpenMessage(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001588 // otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001589}
1590
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001591// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001592bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001593 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1594 if (!bundle_enabled)
1595 return true;
1596
1597 const cricket::ContentGroup* bundle_group =
1598 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1599 ASSERT(bundle_group != NULL);
1600
1601 const cricket::ContentInfos& contents = desc->contents();
1602 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1603 citer != contents.end(); ++citer) {
1604 const cricket::ContentInfo* content = (&*citer);
1605 ASSERT(content != NULL);
1606 if (bundle_group->HasContentName(content->name) &&
1607 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1608 if (!HasRtcpMuxEnabled(content))
1609 return false;
1610 }
1611 }
1612 // RTCP-MUX is enabled in all the contents.
1613 return true;
1614}
1615
1616bool WebRtcSession::HasRtcpMuxEnabled(
1617 const cricket::ContentInfo* content) {
1618 const cricket::MediaContentDescription* description =
1619 static_cast<cricket::MediaContentDescription*>(content->description);
1620 return description->rtcp_mux();
1621}
1622
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001623bool WebRtcSession::ValidateSessionDescription(
1624 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001625 cricket::ContentSource source, std::string* err_desc) {
1626 std::string type;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001627 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001628 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001629 }
1630
1631 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001632 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001633 }
1634
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001635 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001636 Action action = GetAction(sdesc->type());
1637 if (source == cricket::CS_LOCAL) {
1638 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001639 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001640 } else {
1641 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001642 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001643 }
1644
1645 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001646 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001647 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1648 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001649 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001650 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001651 }
1652
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001653 // Verify ice-ufrag and ice-pwd.
1654 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001655 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001656 }
1657
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001658 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001659 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001660 }
1661
1662 // Verify m-lines in Answer when compared against Offer.
1663 if (action == kAnswer) {
1664 const cricket::SessionDescription* offer_desc =
1665 (source == cricket::CS_LOCAL) ? remote_description()->description() :
1666 local_description()->description();
1667 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001668 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001669 }
1670 }
1671
1672 return true;
1673}
1674
1675bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1676 return ((action == kOffer && state() == STATE_INIT) ||
1677 // update local offer
1678 (action == kOffer && state() == STATE_SENTINITIATE) ||
1679 // update the current ongoing session.
1680 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1681 (action == kOffer && state() == STATE_SENTACCEPT) ||
1682 (action == kOffer && state() == STATE_INPROGRESS) ||
1683 // accept remote offer
1684 (action == kAnswer && state() == STATE_RECEIVEDINITIATE) ||
1685 (action == kAnswer && state() == STATE_SENTPRACCEPT) ||
1686 (action == kPrAnswer && state() == STATE_RECEIVEDINITIATE) ||
1687 (action == kPrAnswer && state() == STATE_SENTPRACCEPT));
1688}
1689
1690bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1691 return ((action == kOffer && state() == STATE_INIT) ||
1692 // update remote offer
1693 (action == kOffer && state() == STATE_RECEIVEDINITIATE) ||
1694 // update the current ongoing session
1695 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1696 (action == kOffer && state() == STATE_SENTACCEPT) ||
1697 (action == kOffer && state() == STATE_INPROGRESS) ||
1698 // accept local offer
1699 (action == kAnswer && state() == STATE_SENTINITIATE) ||
1700 (action == kAnswer && state() == STATE_RECEIVEDPRACCEPT) ||
1701 (action == kPrAnswer && state() == STATE_SENTINITIATE) ||
1702 (action == kPrAnswer && state() == STATE_RECEIVEDPRACCEPT));
1703}
1704
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001705std::string WebRtcSession::GetSessionErrorMsg() {
1706 std::ostringstream desc;
1707 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1708 desc << kSessionErrorDesc << error_desc() << ".";
1709 return desc.str();
1710}
1711
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001712// We need to check the local/remote description for the Transport instead of
1713// the session, because a new Transport added during renegotiation may have
1714// them unset while the session has them set from the previous negotiation.
1715// Not doing so may trigger the auto generation of transport description and
1716// mess up DTLS identity information, ICE credential, etc.
1717bool WebRtcSession::ReadyToUseRemoteCandidate(
1718 const IceCandidateInterface* candidate,
1719 const SessionDescriptionInterface* remote_desc,
1720 bool* valid) {
1721 *valid = true;;
1722 cricket::TransportProxy* transport_proxy = NULL;
1723
1724 const SessionDescriptionInterface* current_remote_desc =
1725 remote_desc ? remote_desc : remote_description();
1726
1727 if (!current_remote_desc)
1728 return false;
1729
1730 size_t mediacontent_index =
1731 static_cast<size_t>(candidate->sdp_mline_index());
1732 size_t remote_content_size =
1733 current_remote_desc->description()->contents().size();
1734 if (mediacontent_index >= remote_content_size) {
1735 LOG(LS_ERROR)
1736 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
1737
1738 *valid = false;
1739 return false;
1740 }
1741
1742 cricket::ContentInfo content =
1743 current_remote_desc->description()->contents()[mediacontent_index];
1744 transport_proxy = GetTransportProxy(content.name);
1745
1746 return transport_proxy && transport_proxy->local_description_set() &&
1747 transport_proxy->remote_description_set();
1748}
1749
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001750// Walk through the ConnectionInfos to gather best connection usage
1751// for IPv4 and IPv6.
1752void WebRtcSession::ReportBestConnectionState(cricket::Transport* transport) {
1753 if (!metrics_observer_) {
1754 return;
1755 }
1756
1757 cricket::TransportStats stats;
1758 if (!transport->GetStats(&stats)) {
1759 return;
1760 }
1761
1762 for (cricket::TransportChannelStatsList::const_iterator it =
1763 stats.channel_stats.begin();
1764 it != stats.channel_stats.end(); ++it) {
1765 for (cricket::ConnectionInfos::const_iterator it_info =
1766 it->connection_infos.begin();
1767 it_info != it->connection_infos.end(); ++it_info) {
1768 if (!it_info->best_connection) {
1769 continue;
1770 }
1771 if (it_info->local_candidate.address().family() == AF_INET) {
1772 metrics_observer_->IncrementCounter(kBestConnections_IPv4);
1773 } else if (it_info->local_candidate.address().family() ==
1774 AF_INET6) {
1775 metrics_observer_->IncrementCounter(kBestConnections_IPv6);
1776 } else {
1777 ASSERT(false);
1778 }
1779 return;
1780 }
1781 }
1782}
1783
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001784} // namespace webrtc