blob: 0145243fe12077200c4c7d7b62de63264a6b5aaa [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
bjornv@webrtc.orgc5f69712015-02-04 10:22:14 +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)
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000474 : 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,
bjornv@webrtc.orgc5f69712015-02-04 10:22:14 +0000519 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(
bjornv@webrtc.orgc5f69712015-02-04 10:22:14 +0000662 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 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000840
841 // Set the the ICE connection state to connecting since the connection may
842 // become writable with peer reflexive candidates before any remote candidate
843 // is signaled.
844 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
845 // is to have a new signal the indicates a change in checking state from the
846 // transport and expose a new checking() member from transport that can be
847 // read to determine the current checking state. The existing SignalConnecting
848 // actually means "gathering candidates", so cannot be be used here.
849 if (desc->type() != SessionDescriptionInterface::kOffer &&
850 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
851 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
852 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 return true;
854}
855
856bool WebRtcSession::UpdateSessionState(
857 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 std::string* err_desc) {
859 // If there's already a pending error then no state transition should happen.
860 // But all call-sites should be verifying this before calling us!
861 ASSERT(error() == cricket::BaseSession::ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000862 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000864 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
865 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000866 }
867 SetState(source == cricket::CS_LOCAL ?
868 STATE_SENTINITIATE : STATE_RECEIVEDINITIATE);
869 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000870 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000871 }
872 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000873 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
874 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 }
876 EnableChannels();
877 SetState(source == cricket::CS_LOCAL ?
878 STATE_SENTPRACCEPT : STATE_RECEIVEDPRACCEPT);
879 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000880 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000881 }
882 } else if (action == kAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000883 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
884 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 }
886 MaybeEnableMuxingSupport();
887 EnableChannels();
888 SetState(source == cricket::CS_LOCAL ?
889 STATE_SENTACCEPT : STATE_RECEIVEDACCEPT);
890 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000891 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892 }
893 }
894 return true;
895}
896
897WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
898 if (type == SessionDescriptionInterface::kOffer) {
899 return WebRtcSession::kOffer;
900 } else if (type == SessionDescriptionInterface::kPrAnswer) {
901 return WebRtcSession::kPrAnswer;
902 } else if (type == SessionDescriptionInterface::kAnswer) {
903 return WebRtcSession::kAnswer;
904 }
905 ASSERT(false && "unknown action type");
906 return WebRtcSession::kOffer;
907}
908
909bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
910 if (state() == STATE_INIT) {
911 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
912 << "without any offer (local or remote) "
913 << "session description.";
914 return false;
915 }
916
917 if (!candidate) {
918 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL";
919 return false;
920 }
921
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +0000922 bool valid = false;
923 if (!ReadyToUseRemoteCandidate(candidate, NULL, &valid)) {
924 if (valid) {
925 LOG(LS_INFO) << "ProcessIceMessage: Candidate saved";
926 saved_candidates_.push_back(
927 new JsepIceCandidate(candidate->sdp_mid(),
928 candidate->sdp_mline_index(),
929 candidate->candidate()));
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000930 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +0000931 return valid;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000932 }
933
934 // Add this candidate to the remote session description.
935 if (!remote_desc_->AddCandidate(candidate)) {
936 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used";
937 return false;
938 }
939
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000940 return UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000941}
942
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000943bool WebRtcSession::SetIceTransports(
944 PeerConnectionInterface::IceTransportsType type) {
945 return port_allocator()->set_candidate_filter(
946 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000947}
948
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000949bool WebRtcSession::GetLocalTrackIdBySsrc(uint32 ssrc, std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950 if (!BaseSession::local_description())
951 return false;
952 return webrtc::GetTrackIdBySsrc(
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000953 BaseSession::local_description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954}
955
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000956bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32 ssrc, std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000957 if (!BaseSession::remote_description())
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000958 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000959 return webrtc::GetTrackIdBySsrc(
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000960 BaseSession::remote_description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000961}
962
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000963std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000965 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000966 return desc.str();
967}
968
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000969void WebRtcSession::SetAudioPlayout(uint32 ssrc, bool enable,
970 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971 ASSERT(signaling_thread()->IsCurrent());
972 if (!voice_channel_) {
973 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
974 return;
975 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000976 if (!voice_channel_->SetRemoteRenderer(ssrc, renderer)) {
977 // SetRenderer() can fail if the ssrc does not match any playout channel.
978 LOG(LS_ERROR) << "SetAudioPlayout: ssrc is incorrect: " << ssrc;
979 return;
980 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000981 if (!voice_channel_->SetOutputScaling(ssrc, enable ? 1 : 0, enable ? 1 : 0)) {
982 // Allow that SetOutputScaling fail if |enable| is false but assert
983 // otherwise. This in the normal case when the underlying media channel has
984 // already been deleted.
985 ASSERT(enable == false);
986 }
987}
988
989void WebRtcSession::SetAudioSend(uint32 ssrc, bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000990 const cricket::AudioOptions& options,
991 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000992 ASSERT(signaling_thread()->IsCurrent());
993 if (!voice_channel_) {
994 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
995 return;
996 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000997 if (!voice_channel_->SetLocalRenderer(ssrc, renderer)) {
998 // SetRenderer() can fail if the ssrc does not match any send channel.
999 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
1000 return;
1001 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001002 if (!voice_channel_->MuteStream(ssrc, !enable)) {
1003 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1004 // This in the normal case when the underlying media channel has already
1005 // been deleted.
1006 ASSERT(enable == false);
1007 return;
1008 }
1009 if (enable)
1010 voice_channel_->SetChannelOptions(options);
1011}
1012
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001013void WebRtcSession::SetAudioPlayoutVolume(uint32 ssrc, double volume) {
1014 ASSERT(signaling_thread()->IsCurrent());
1015 ASSERT(volume >= 0 && volume <= 10);
1016 if (!voice_channel_) {
1017 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1018 return;
1019 }
1020
1021 if (!voice_channel_->SetOutputScaling(ssrc, volume, volume))
1022 ASSERT(false);
1023}
1024
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001025bool WebRtcSession::SetCaptureDevice(uint32 ssrc,
1026 cricket::VideoCapturer* camera) {
1027 ASSERT(signaling_thread()->IsCurrent());
1028
1029 if (!video_channel_.get()) {
1030 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1031 // support video.
1032 LOG(LS_WARNING) << "Video not used in this call.";
1033 return false;
1034 }
1035 if (!video_channel_->SetCapturer(ssrc, camera)) {
1036 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1037 // This in the normal case when the underlying media channel has already
1038 // been deleted.
1039 ASSERT(camera == NULL);
1040 return false;
1041 }
1042 return true;
1043}
1044
1045void WebRtcSession::SetVideoPlayout(uint32 ssrc,
1046 bool enable,
1047 cricket::VideoRenderer* renderer) {
1048 ASSERT(signaling_thread()->IsCurrent());
1049 if (!video_channel_) {
1050 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1051 return;
1052 }
1053 if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) {
1054 // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise.
1055 // This in the normal case when the underlying media channel has already
1056 // been deleted.
1057 ASSERT(renderer == NULL);
1058 }
1059}
1060
1061void WebRtcSession::SetVideoSend(uint32 ssrc, bool enable,
1062 const cricket::VideoOptions* options) {
1063 ASSERT(signaling_thread()->IsCurrent());
1064 if (!video_channel_) {
1065 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1066 return;
1067 }
1068 if (!video_channel_->MuteStream(ssrc, !enable)) {
1069 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1070 // This in the normal case when the underlying media channel has already
1071 // been deleted.
1072 ASSERT(enable == false);
1073 return;
1074 }
1075 if (enable && options)
1076 video_channel_->SetChannelOptions(*options);
1077}
1078
1079bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1080 ASSERT(signaling_thread()->IsCurrent());
1081 if (!voice_channel_) {
1082 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1083 return false;
1084 }
1085 uint32 send_ssrc = 0;
1086 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1087 // exists.
1088 if (!GetAudioSsrcByTrackId(BaseSession::local_description(), track_id,
1089 &send_ssrc)) {
1090 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1091 return false;
1092 }
1093 return voice_channel_->CanInsertDtmf();
1094}
1095
1096bool WebRtcSession::InsertDtmf(const std::string& track_id,
1097 int code, int duration) {
1098 ASSERT(signaling_thread()->IsCurrent());
1099 if (!voice_channel_) {
1100 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1101 return false;
1102 }
1103 uint32 send_ssrc = 0;
1104 if (!VERIFY(GetAudioSsrcByTrackId(BaseSession::local_description(),
1105 track_id, &send_ssrc))) {
1106 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1107 return false;
1108 }
1109 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration,
1110 cricket::DF_SEND)) {
1111 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1112 return false;
1113 }
1114 return true;
1115}
1116
1117sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
1118 return &SignalVoiceChannelDestroyed;
1119}
1120
wu@webrtc.org78187522013-10-07 23:32:02 +00001121bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001122 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001123 cricket::SendDataResult* result) {
1124 if (!data_channel_.get()) {
1125 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1126 return false;
1127 }
1128 return data_channel_->SendData(params, payload, result);
1129}
1130
1131bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
1132 if (!data_channel_.get()) {
1133 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1134 return false;
1135 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001136 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1137 &DataChannel::OnChannelReady);
1138 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1139 &DataChannel::OnDataReceived);
wu@webrtc.org78187522013-10-07 23:32:02 +00001140 return true;
1141}
1142
1143void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001144 if (!data_channel_.get()) {
1145 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1146 return;
1147 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001148 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1149 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
1150}
1151
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001152void WebRtcSession::AddSctpDataStream(int sid) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001153 if (!data_channel_.get()) {
1154 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1155 return;
1156 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001157 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1158 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001159}
1160
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001161void WebRtcSession::RemoveSctpDataStream(int sid) {
1162 mediastream_signaling_->RemoveSctpDataChannel(sid);
jiayl@webrtc.org2eaac182014-06-17 16:02:46 +00001163
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001164 if (!data_channel_.get()) {
1165 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1166 << "NULL.";
1167 return;
1168 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001169 data_channel_->RemoveRecvStream(sid);
1170 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001171}
1172
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001173bool WebRtcSession::ReadyToSendData() const {
1174 return data_channel_.get() && data_channel_->ready_to_send_data();
1175}
1176
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001177rtc::scoped_refptr<DataChannel> WebRtcSession::CreateDataChannel(
wu@webrtc.org78187522013-10-07 23:32:02 +00001178 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001179 const InternalDataChannelInit* config) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001180 if (state() == STATE_RECEIVEDTERMINATE) {
1181 return NULL;
1182 }
1183 if (data_channel_type_ == cricket::DCT_NONE) {
1184 LOG(LS_ERROR) << "CreateDataChannel: Data is not supported in this call.";
1185 return NULL;
1186 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001187 InternalDataChannelInit new_config =
1188 config ? (*config) : InternalDataChannelInit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001189 if (data_channel_type_ == cricket::DCT_SCTP) {
1190 if (new_config.id < 0) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001191 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001192 if (GetSslRole(&role) &&
1193 !mediastream_signaling_->AllocateSctpSid(role, &new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001194 LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel.";
1195 return NULL;
1196 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001197 } else if (!mediastream_signaling_->IsSctpSidAvailable(new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001198 LOG(LS_ERROR) << "Failed to create a SCTP data channel "
1199 << "because the id is already in use or out of range.";
1200 return NULL;
1201 }
1202 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001203
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001204 rtc::scoped_refptr<DataChannel> channel(DataChannel::Create(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001205 this, data_channel_type_, label, new_config));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001206 if (channel && !mediastream_signaling_->AddDataChannel(channel))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001207 return NULL;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001208
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001209 return channel;
1210}
1211
1212cricket::DataChannelType WebRtcSession::data_channel_type() const {
1213 return data_channel_type_;
1214}
1215
wu@webrtc.org91053e72013-08-10 07:18:04 +00001216bool WebRtcSession::IceRestartPending() const {
1217 return ice_restart_latch_->Get();
1218}
1219
1220void WebRtcSession::ResetIceRestartLatch() {
1221 ice_restart_latch_->Reset();
1222}
1223
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001224void WebRtcSession::OnIdentityReady(rtc::SSLIdentity* identity) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001225 SetIdentity(identity);
1226}
1227
1228bool WebRtcSession::waiting_for_identity() const {
1229 return webrtc_session_desc_factory_->waiting_for_identity();
1230}
1231
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001232void WebRtcSession::SetIceConnectionState(
1233 PeerConnectionInterface::IceConnectionState state) {
1234 if (ice_connection_state_ == state) {
1235 return;
1236 }
1237
1238 // ASSERT that the requested transition is allowed. Note that
1239 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1240 // within PeerConnection). This switch statement should compile away when
1241 // ASSERTs are disabled.
1242 switch (ice_connection_state_) {
1243 case PeerConnectionInterface::kIceConnectionNew:
1244 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1245 break;
1246 case PeerConnectionInterface::kIceConnectionChecking:
1247 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1248 state == PeerConnectionInterface::kIceConnectionConnected);
1249 break;
1250 case PeerConnectionInterface::kIceConnectionConnected:
1251 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1252 state == PeerConnectionInterface::kIceConnectionChecking ||
1253 state == PeerConnectionInterface::kIceConnectionCompleted);
1254 break;
1255 case PeerConnectionInterface::kIceConnectionCompleted:
1256 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1257 state == PeerConnectionInterface::kIceConnectionDisconnected);
1258 break;
1259 case PeerConnectionInterface::kIceConnectionFailed:
1260 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1261 break;
1262 case PeerConnectionInterface::kIceConnectionDisconnected:
1263 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1264 state == PeerConnectionInterface::kIceConnectionConnected ||
1265 state == PeerConnectionInterface::kIceConnectionCompleted ||
1266 state == PeerConnectionInterface::kIceConnectionFailed);
1267 break;
1268 case PeerConnectionInterface::kIceConnectionClosed:
1269 ASSERT(false);
1270 break;
1271 default:
1272 ASSERT(false);
1273 break;
1274 }
1275
1276 ice_connection_state_ = state;
1277 if (ice_observer_) {
1278 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1279 }
1280}
1281
1282void WebRtcSession::OnTransportRequestSignaling(
1283 cricket::Transport* transport) {
1284 ASSERT(signaling_thread()->IsCurrent());
1285 transport->OnSignalingReady();
1286 if (ice_observer_) {
1287 ice_observer_->OnIceGatheringChange(
1288 PeerConnectionInterface::kIceGatheringGathering);
1289 }
1290}
1291
1292void WebRtcSession::OnTransportConnecting(cricket::Transport* transport) {
1293 ASSERT(signaling_thread()->IsCurrent());
1294 // start monitoring for the write state of the transport.
1295 OnTransportWritable(transport);
1296}
1297
1298void WebRtcSession::OnTransportWritable(cricket::Transport* transport) {
1299 ASSERT(signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001300 if (transport->all_channels_writable()) {
henrike@webrtc.org05376342014-03-10 15:53:12 +00001301 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001302 } else if (transport->HasChannels()) {
1303 // If the current state is Connected or Completed, then there were writable
1304 // channels but now there are not, so the next state must be Disconnected.
1305 if (ice_connection_state_ ==
1306 PeerConnectionInterface::kIceConnectionConnected ||
1307 ice_connection_state_ ==
1308 PeerConnectionInterface::kIceConnectionCompleted) {
1309 SetIceConnectionState(
1310 PeerConnectionInterface::kIceConnectionDisconnected);
1311 }
1312 }
1313}
1314
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001315void WebRtcSession::OnTransportCompleted(cricket::Transport* transport) {
1316 ASSERT(signaling_thread()->IsCurrent());
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001317 PeerConnectionInterface::IceConnectionState old_state = ice_connection_state_;
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001318 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001319 // Only report once when Ice connection is completed.
1320 if (old_state != PeerConnectionInterface::kIceConnectionCompleted) {
1321 ReportBestConnectionState(transport);
1322 }
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001323}
1324
1325void WebRtcSession::OnTransportFailed(cricket::Transport* transport) {
1326 ASSERT(signaling_thread()->IsCurrent());
1327 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1328}
1329
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001330void WebRtcSession::OnTransportProxyCandidatesReady(
1331 cricket::TransportProxy* proxy, const cricket::Candidates& candidates) {
1332 ASSERT(signaling_thread()->IsCurrent());
1333 ProcessNewLocalCandidate(proxy->content_name(), candidates);
1334}
1335
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001336void WebRtcSession::OnCandidatesAllocationDone() {
1337 ASSERT(signaling_thread()->IsCurrent());
1338 if (ice_observer_) {
1339 ice_observer_->OnIceGatheringChange(
1340 PeerConnectionInterface::kIceGatheringComplete);
1341 ice_observer_->OnIceComplete();
1342 }
1343}
1344
1345// Enabling voice and video channel.
1346void WebRtcSession::EnableChannels() {
1347 if (voice_channel_ && !voice_channel_->enabled())
1348 voice_channel_->Enable(true);
1349
1350 if (video_channel_ && !video_channel_->enabled())
1351 video_channel_->Enable(true);
1352
1353 if (data_channel_.get() && !data_channel_->enabled())
1354 data_channel_->Enable(true);
1355}
1356
1357void WebRtcSession::ProcessNewLocalCandidate(
1358 const std::string& content_name,
1359 const cricket::Candidates& candidates) {
1360 int sdp_mline_index;
1361 if (!GetLocalCandidateMediaIndex(content_name, &sdp_mline_index)) {
1362 LOG(LS_ERROR) << "ProcessNewLocalCandidate: content name "
1363 << content_name << " not found";
1364 return;
1365 }
1366
1367 for (cricket::Candidates::const_iterator citer = candidates.begin();
1368 citer != candidates.end(); ++citer) {
1369 // Use content_name as the candidate media id.
1370 JsepIceCandidate candidate(content_name, sdp_mline_index, *citer);
1371 if (ice_observer_) {
1372 ice_observer_->OnIceCandidate(&candidate);
1373 }
1374 if (local_desc_) {
1375 local_desc_->AddCandidate(&candidate);
1376 }
1377 }
1378}
1379
1380// Returns the media index for a local ice candidate given the content name.
1381bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1382 int* sdp_mline_index) {
1383 if (!BaseSession::local_description() || !sdp_mline_index)
1384 return false;
1385
1386 bool content_found = false;
1387 const ContentInfos& contents = BaseSession::local_description()->contents();
1388 for (size_t index = 0; index < contents.size(); ++index) {
1389 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001390 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001391 content_found = true;
1392 break;
1393 }
1394 }
1395 return content_found;
1396}
1397
1398bool WebRtcSession::UseCandidatesInSessionDescription(
1399 const SessionDescriptionInterface* remote_desc) {
1400 if (!remote_desc)
1401 return true;
1402 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001403
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001404 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1405 const IceCandidateCollection* candidates = remote_desc->candidates(m);
1406 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001407 const IceCandidateInterface* candidate = candidates->at(n);
1408 bool valid = false;
1409 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1410 if (valid) {
1411 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Candidate saved.";
1412 saved_candidates_.push_back(
1413 new JsepIceCandidate(candidate->sdp_mid(),
1414 candidate->sdp_mline_index(),
1415 candidate->candidate()));
1416 }
1417 continue;
1418 }
1419
1420 ret = UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001421 if (!ret)
1422 break;
1423 }
1424 }
1425 return ret;
1426}
1427
1428bool WebRtcSession::UseCandidate(
1429 const IceCandidateInterface* candidate) {
1430
1431 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
1432 size_t remote_content_size =
1433 BaseSession::remote_description()->contents().size();
1434 if (mediacontent_index >= remote_content_size) {
1435 LOG(LS_ERROR)
1436 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1437 return false;
1438 }
1439
1440 cricket::ContentInfo content =
1441 BaseSession::remote_description()->contents()[mediacontent_index];
1442 std::vector<cricket::Candidate> candidates;
1443 candidates.push_back(candidate->candidate());
1444 // Invoking BaseSession method to handle remote candidates.
1445 std::string error;
1446 if (OnRemoteCandidates(content.name, candidates, &error)) {
1447 // Candidates successfully submitted for checking.
1448 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1449 ice_connection_state_ ==
1450 PeerConnectionInterface::kIceConnectionDisconnected) {
1451 // If state is New, then the session has just gotten its first remote ICE
1452 // candidates, so go to Checking.
1453 // If state is Disconnected, the session is re-using old candidates or
1454 // receiving additional ones, so go to Checking.
1455 // If state is Connected, stay Connected.
1456 // TODO(bemasc): If state is Connected, and the new candidates are for a
1457 // newly added transport, then the state actually _should_ move to
1458 // checking. Add a way to distinguish that case.
1459 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1460 }
1461 // TODO(bemasc): If state is Completed, go back to Connected.
1462 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001463 if (!error.empty()) {
1464 LOG(LS_WARNING) << error;
1465 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001466 }
1467 return true;
1468}
1469
1470void WebRtcSession::RemoveUnusedChannelsAndTransports(
1471 const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001472 // Destroy video_channel_ first since it may have a pointer to the
1473 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001474 const cricket::ContentInfo* video_info =
1475 cricket::GetFirstVideoContent(desc);
1476 if ((!video_info || video_info->rejected) && video_channel_) {
1477 mediastream_signaling_->OnVideoChannelClose();
1478 SignalVideoChannelDestroyed();
1479 const std::string content_name = video_channel_->content_name();
1480 channel_manager_->DestroyVideoChannel(video_channel_.release());
decurtis@webrtc.orge2506672015-02-03 23:18:39 +00001481 DestroyTransportProxyWhenUnused(content_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001482 }
1483
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001484 const cricket::ContentInfo* voice_info =
1485 cricket::GetFirstAudioContent(desc);
1486 if ((!voice_info || voice_info->rejected) && voice_channel_) {
1487 mediastream_signaling_->OnAudioChannelClose();
1488 SignalVoiceChannelDestroyed();
1489 const std::string content_name = voice_channel_->content_name();
1490 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
decurtis@webrtc.orge2506672015-02-03 23:18:39 +00001491 DestroyTransportProxyWhenUnused(content_name);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001492 }
1493
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001494 const cricket::ContentInfo* data_info =
1495 cricket::GetFirstDataContent(desc);
1496 if ((!data_info || data_info->rejected) && data_channel_) {
1497 mediastream_signaling_->OnDataChannelClose();
1498 SignalDataChannelDestroyed();
1499 const std::string content_name = data_channel_->content_name();
1500 channel_manager_->DestroyDataChannel(data_channel_.release());
decurtis@webrtc.orge2506672015-02-03 23:18:39 +00001501 DestroyTransportProxyWhenUnused(content_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001502 }
1503}
1504
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001505// TODO(mallinath) - Add a correct error code if the channels are not creatued
1506// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001507bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
1508 // Disabling the BUNDLE flag in PortAllocator if offer disabled it.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001509 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1510 if (state() == STATE_INIT && !bundle_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001511 port_allocator()->set_flags(port_allocator()->flags() &
1512 ~cricket::PORTALLOCATOR_ENABLE_BUNDLE);
1513 }
1514
1515 // Creating the media channels and transport proxies.
1516 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1517 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001518 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001519 LOG(LS_ERROR) << "Failed to create voice channel.";
1520 return false;
1521 }
1522 }
1523
1524 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1525 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001526 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001527 LOG(LS_ERROR) << "Failed to create video channel.";
1528 return false;
1529 }
1530 }
1531
1532 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1533 if (data_channel_type_ != cricket::DCT_NONE &&
1534 data && !data->rejected && !data_channel_.get()) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001535 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001536 LOG(LS_ERROR) << "Failed to create data channel.";
1537 return false;
1538 }
1539 }
1540
1541 return true;
1542}
1543
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001544bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001545 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001546 this, content->name, true));
wu@webrtc.orgde305012013-10-31 15:40:38 +00001547 if (!voice_channel_.get())
1548 return false;
1549
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00001550 voice_channel_->SetChannelOptions(audio_options_);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001551 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001552}
1553
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001554bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001555 video_channel_.reset(channel_manager_->CreateVideoChannel(
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +00001556 this, content->name, true, video_options_, voice_channel_.get()));
1557 return video_channel_.get() != NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001558}
1559
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001560bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001561 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001562 data_channel_.reset(channel_manager_->CreateDataChannel(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001563 this, content->name, !sctp, data_channel_type_));
wu@webrtc.org91053e72013-08-10 07:18:04 +00001564 if (!data_channel_.get()) {
1565 return false;
1566 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001567 if (sctp) {
1568 mediastream_signaling_->OnDataTransportCreatedForSctp();
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001569 data_channel_->SignalDataReceived.connect(
1570 this, &WebRtcSession::OnDataChannelMessageReceived);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00001571 data_channel_->SignalStreamClosedRemotely.connect(
1572 mediastream_signaling_,
1573 &MediaStreamSignaling::OnRemoteSctpDataChannelClosed);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001574 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001575 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001576}
1577
1578void WebRtcSession::CopySavedCandidates(
1579 SessionDescriptionInterface* dest_desc) {
1580 if (!dest_desc) {
1581 ASSERT(false);
1582 return;
1583 }
1584 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
1585 dest_desc->AddCandidate(saved_candidates_[i]);
1586 delete saved_candidates_[i];
1587 }
1588 saved_candidates_.clear();
1589}
1590
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001591void WebRtcSession::OnDataChannelMessageReceived(
1592 cricket::DataChannel* channel,
1593 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001594 const rtc::Buffer& payload) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001595 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001596 if (params.type == cricket::DMT_CONTROL &&
1597 mediastream_signaling_->IsSctpSidAvailable(params.ssrc)) {
1598 // Received CONTROL on unused sid, process as an OPEN message.
1599 mediastream_signaling_->AddDataChannelFromOpenMessage(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001601 // otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001602}
1603
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001604// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001605bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001606 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1607 if (!bundle_enabled)
1608 return true;
1609
1610 const cricket::ContentGroup* bundle_group =
1611 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1612 ASSERT(bundle_group != NULL);
1613
1614 const cricket::ContentInfos& contents = desc->contents();
1615 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1616 citer != contents.end(); ++citer) {
1617 const cricket::ContentInfo* content = (&*citer);
1618 ASSERT(content != NULL);
1619 if (bundle_group->HasContentName(content->name) &&
1620 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1621 if (!HasRtcpMuxEnabled(content))
1622 return false;
1623 }
1624 }
1625 // RTCP-MUX is enabled in all the contents.
1626 return true;
1627}
1628
1629bool WebRtcSession::HasRtcpMuxEnabled(
1630 const cricket::ContentInfo* content) {
1631 const cricket::MediaContentDescription* description =
1632 static_cast<cricket::MediaContentDescription*>(content->description);
1633 return description->rtcp_mux();
1634}
1635
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001636bool WebRtcSession::ValidateSessionDescription(
1637 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001638 cricket::ContentSource source, std::string* err_desc) {
1639 std::string type;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001640 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001641 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001642 }
1643
1644 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001645 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001646 }
1647
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001648 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001649 Action action = GetAction(sdesc->type());
1650 if (source == cricket::CS_LOCAL) {
1651 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001652 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001653 } else {
1654 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001655 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001656 }
1657
1658 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001659 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001660 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1661 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001662 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001663 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001664 }
1665
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001666 // Verify ice-ufrag and ice-pwd.
1667 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001668 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001669 }
1670
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001671 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001672 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001673 }
1674
1675 // Verify m-lines in Answer when compared against Offer.
1676 if (action == kAnswer) {
1677 const cricket::SessionDescription* offer_desc =
1678 (source == cricket::CS_LOCAL) ? remote_description()->description() :
1679 local_description()->description();
1680 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001681 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001682 }
1683 }
1684
1685 return true;
1686}
1687
1688bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1689 return ((action == kOffer && state() == STATE_INIT) ||
1690 // update local offer
1691 (action == kOffer && state() == STATE_SENTINITIATE) ||
1692 // update the current ongoing session.
1693 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1694 (action == kOffer && state() == STATE_SENTACCEPT) ||
1695 (action == kOffer && state() == STATE_INPROGRESS) ||
1696 // accept remote offer
1697 (action == kAnswer && state() == STATE_RECEIVEDINITIATE) ||
1698 (action == kAnswer && state() == STATE_SENTPRACCEPT) ||
1699 (action == kPrAnswer && state() == STATE_RECEIVEDINITIATE) ||
1700 (action == kPrAnswer && state() == STATE_SENTPRACCEPT));
1701}
1702
1703bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1704 return ((action == kOffer && state() == STATE_INIT) ||
1705 // update remote offer
1706 (action == kOffer && state() == STATE_RECEIVEDINITIATE) ||
1707 // update the current ongoing session
1708 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1709 (action == kOffer && state() == STATE_SENTACCEPT) ||
1710 (action == kOffer && state() == STATE_INPROGRESS) ||
1711 // accept local offer
1712 (action == kAnswer && state() == STATE_SENTINITIATE) ||
1713 (action == kAnswer && state() == STATE_RECEIVEDPRACCEPT) ||
1714 (action == kPrAnswer && state() == STATE_SENTINITIATE) ||
1715 (action == kPrAnswer && state() == STATE_RECEIVEDPRACCEPT));
1716}
1717
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001718std::string WebRtcSession::GetSessionErrorMsg() {
1719 std::ostringstream desc;
1720 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1721 desc << kSessionErrorDesc << error_desc() << ".";
1722 return desc.str();
1723}
1724
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001725// We need to check the local/remote description for the Transport instead of
1726// the session, because a new Transport added during renegotiation may have
1727// them unset while the session has them set from the previous negotiation.
1728// Not doing so may trigger the auto generation of transport description and
1729// mess up DTLS identity information, ICE credential, etc.
1730bool WebRtcSession::ReadyToUseRemoteCandidate(
1731 const IceCandidateInterface* candidate,
1732 const SessionDescriptionInterface* remote_desc,
1733 bool* valid) {
1734 *valid = true;;
1735 cricket::TransportProxy* transport_proxy = NULL;
1736
1737 const SessionDescriptionInterface* current_remote_desc =
1738 remote_desc ? remote_desc : remote_description();
1739
1740 if (!current_remote_desc)
1741 return false;
1742
1743 size_t mediacontent_index =
1744 static_cast<size_t>(candidate->sdp_mline_index());
1745 size_t remote_content_size =
1746 current_remote_desc->description()->contents().size();
1747 if (mediacontent_index >= remote_content_size) {
1748 LOG(LS_ERROR)
1749 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
1750
1751 *valid = false;
1752 return false;
1753 }
1754
1755 cricket::ContentInfo content =
1756 current_remote_desc->description()->contents()[mediacontent_index];
1757 transport_proxy = GetTransportProxy(content.name);
1758
1759 return transport_proxy && transport_proxy->local_description_set() &&
1760 transport_proxy->remote_description_set();
1761}
1762
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001763// Walk through the ConnectionInfos to gather best connection usage
1764// for IPv4 and IPv6.
1765void WebRtcSession::ReportBestConnectionState(cricket::Transport* transport) {
1766 if (!metrics_observer_) {
1767 return;
1768 }
1769
1770 cricket::TransportStats stats;
1771 if (!transport->GetStats(&stats)) {
1772 return;
1773 }
1774
1775 for (cricket::TransportChannelStatsList::const_iterator it =
1776 stats.channel_stats.begin();
1777 it != stats.channel_stats.end(); ++it) {
1778 for (cricket::ConnectionInfos::const_iterator it_info =
1779 it->connection_infos.begin();
1780 it_info != it->connection_infos.end(); ++it_info) {
1781 if (!it_info->best_connection) {
1782 continue;
1783 }
1784 if (it_info->local_candidate.address().family() == AF_INET) {
1785 metrics_observer_->IncrementCounter(kBestConnections_IPv4);
1786 } else if (it_info->local_candidate.address().family() ==
1787 AF_INET6) {
1788 metrics_observer_->IncrementCounter(kBestConnections_IPv6);
1789 } else {
1790 ASSERT(false);
1791 }
1792 return;
1793 }
1794 }
1795}
1796
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001797} // namespace webrtc