blob: dfe8be9ed52d669b28e4dcd7733e26ed25ed285b [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
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000469WebRtcSession::WebRtcSession(
470 cricket::ChannelManager* channel_manager,
471 rtc::Thread* signaling_thread,
472 rtc::Thread* worker_thread,
473 cricket::PortAllocator* port_allocator,
474 MediaStreamSignaling* mediastream_signaling)
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000475 : cricket::BaseSession(signaling_thread,
476 worker_thread,
477 port_allocator,
478 rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX),
479 cricket::NS_JINGLE_RTP,
480 false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481 // RFC 3264: The numeric value of the session id and version in the
482 // o line MUST be representable with a "64 bit signed integer".
483 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
484 channel_manager_(channel_manager),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 mediastream_signaling_(mediastream_signaling),
486 ice_observer_(NULL),
487 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000488 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000489 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000491 ice_restart_latch_(new IceRestartAnswerLatch),
492 metrics_observer_(NULL) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493}
494
495WebRtcSession::~WebRtcSession() {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000496 // Destroy video_channel_ first since it may have a pointer to the
497 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498 if (video_channel_.get()) {
499 SignalVideoChannelDestroyed();
500 channel_manager_->DestroyVideoChannel(video_channel_.release());
501 }
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000502 if (voice_channel_.get()) {
503 SignalVoiceChannelDestroyed();
504 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
505 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000506 if (data_channel_.get()) {
507 SignalDataChannelDestroyed();
508 channel_manager_->DestroyDataChannel(data_channel_.release());
509 }
510 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
511 delete saved_candidates_[i];
512 }
513 delete identity();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514}
515
wu@webrtc.org91053e72013-08-10 07:18:04 +0000516bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000517 const PeerConnectionFactoryInterface::Options& options,
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000518 const MediaConstraintsInterface* constraints,
519 DTLSIdentityServiceInterface* dtls_identity_service,
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000520 PeerConnectionInterface::IceTransportsType ice_transport_type,
521 PeerConnectionInterface::BundlePolicy bundle_policy) {
522 bundle_policy_ = bundle_policy;
523
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 // TODO(perkj): Take |constraints| into consideration. Return false if not all
525 // mandatory constraints can be fulfilled. Note that |constraints|
526 // can be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527 bool value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000528
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000529 if (options.disable_encryption) {
530 dtls_enabled_ = false;
531 } else {
532 // Enable DTLS by default if |dtls_identity_service| is valid.
533 dtls_enabled_ = (dtls_identity_service != NULL);
534 // |constraints| can override the default |dtls_enabled_| value.
535 if (FindConstraint(
536 constraints,
537 MediaConstraintsInterface::kEnableDtlsSrtp,
538 &value, NULL)) {
539 dtls_enabled_ = value;
540 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000541 }
542
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000544 // It takes precendence over the disable_sctp_data_channels
545 // PeerConnectionFactoryInterface::Options.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 if (FindConstraint(
547 constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
548 &value, NULL) && value) {
549 LOG(LS_INFO) << "Allowing RTP data engine.";
550 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000551 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000552 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000553 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000554 LOG(LS_INFO) << "Allowing SCTP data engine.";
555 data_channel_type_ = cricket::DCT_SCTP;
556 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 }
558 if (data_channel_type_ != cricket::DCT_NONE) {
559 mediastream_signaling_->SetDataChannelFactory(this);
560 }
561
wu@webrtc.orgde305012013-10-31 15:40:38 +0000562 // Find DSCP constraint.
563 if (FindConstraint(
564 constraints,
565 MediaConstraintsInterface::kEnableDscp,
566 &value, NULL)) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000567 audio_options_.dscp.Set(value);
568 video_options_.dscp.Set(value);
569 }
570
571 // Find Suspend Below Min Bitrate constraint.
572 if (FindConstraint(
573 constraints,
574 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
575 &value,
576 NULL)) {
577 video_options_.suspend_below_min_bitrate.Set(value);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000578 }
579
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000580 SetOptionFromOptionalConstraint(constraints,
581 MediaConstraintsInterface::kScreencastMinBitrate,
582 &video_options_.screencast_min_bitrate);
583
584 // Find constraints for cpu overuse detection.
585 SetOptionFromOptionalConstraint(constraints,
586 MediaConstraintsInterface::kCpuUnderuseThreshold,
587 &video_options_.cpu_underuse_threshold);
588 SetOptionFromOptionalConstraint(constraints,
589 MediaConstraintsInterface::kCpuOveruseThreshold,
590 &video_options_.cpu_overuse_threshold);
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000591 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000592 MediaConstraintsInterface::kCpuOveruseDetection,
593 &video_options_.cpu_overuse_detection);
594 SetOptionFromOptionalConstraint(constraints,
595 MediaConstraintsInterface::kCpuOveruseEncodeUsage,
596 &video_options_.cpu_overuse_encode_usage);
597 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000598 MediaConstraintsInterface::kCpuUnderuseEncodeRsdThreshold,
599 &video_options_.cpu_underuse_encode_rsd_threshold);
600 SetOptionFromOptionalConstraint(constraints,
601 MediaConstraintsInterface::kCpuOveruseEncodeRsdThreshold,
602 &video_options_.cpu_overuse_encode_rsd_threshold);
buildbot@webrtc.orgdb563902014-06-13 13:05:48 +0000603
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000604 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000605 MediaConstraintsInterface::kNumUnsignalledRecvStreams,
606 &video_options_.unsignalled_recv_stream_limit);
607 if (video_options_.unsignalled_recv_stream_limit.IsSet()) {
608 int stream_limit;
609 video_options_.unsignalled_recv_stream_limit.Get(&stream_limit);
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000610 stream_limit = std::min(kMaxUnsignalledRecvStreams, stream_limit);
611 stream_limit = std::max(0, stream_limit);
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000612 video_options_.unsignalled_recv_stream_limit.Set(stream_limit);
613 }
614
615 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000616 MediaConstraintsInterface::kHighStartBitrate,
617 &video_options_.video_start_bitrate);
wu@webrtc.orgcfe5e9c2014-03-27 17:03:58 +0000618
619 if (FindConstraint(
620 constraints,
621 MediaConstraintsInterface::kVeryHighBitrate,
622 &value,
623 NULL)) {
624 video_options_.video_highest_bitrate.Set(
625 cricket::VideoOptions::VERY_HIGH);
626 } else if (FindConstraint(
627 constraints,
628 MediaConstraintsInterface::kHighBitrate,
629 &value,
630 NULL)) {
631 video_options_.video_highest_bitrate.Set(
632 cricket::VideoOptions::HIGH);
633 }
634
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000635 SetOptionFromOptionalConstraint(constraints,
636 MediaConstraintsInterface::kCombinedAudioVideoBwe,
637 &audio_options_.combined_audio_video_bwe);
638
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 const cricket::VideoCodec default_codec(
640 JsepSessionDescription::kDefaultVideoCodecId,
641 JsepSessionDescription::kDefaultVideoCodecName,
642 JsepSessionDescription::kMaxVideoCodecWidth,
643 JsepSessionDescription::kMaxVideoCodecHeight,
644 JsepSessionDescription::kDefaultVideoCodecFramerate,
645 JsepSessionDescription::kDefaultVideoCodecPreference);
646 channel_manager_->SetDefaultVideoEncoderConfig(
647 cricket::VideoEncoderConfig(default_codec));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000648
649 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
650 signaling_thread(),
651 channel_manager_,
652 mediastream_signaling_,
653 dtls_identity_service,
654 this,
655 id(),
656 data_channel_type_,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000657 dtls_enabled_));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000658
659 webrtc_session_desc_factory_->SignalIdentityReady.connect(
660 this, &WebRtcSession::OnIdentityReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000661
wu@webrtc.org97077a32013-10-25 21:18:33 +0000662 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000663 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000664 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000665 port_allocator()->set_candidate_filter(
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000666 ConvertIceTransportTypeToCandidateFilter(ice_transport_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667 return true;
668}
669
670void WebRtcSession::Terminate() {
671 SetState(STATE_RECEIVEDTERMINATE);
672 RemoveUnusedChannelsAndTransports(NULL);
673 ASSERT(voice_channel_.get() == NULL);
674 ASSERT(video_channel_.get() == NULL);
675 ASSERT(data_channel_.get() == NULL);
676}
677
678bool WebRtcSession::StartCandidatesAllocation() {
679 // SpeculativelyConnectTransportChannels, will call ConnectChannels method
680 // from TransportProxy to start gathering ice candidates.
681 SpeculativelyConnectAllTransportChannels();
682 if (!saved_candidates_.empty()) {
683 // If there are saved candidates which arrived before local description is
684 // set, copy those to remote description.
685 CopySavedCandidates(remote_desc_.get());
686 }
687 // Push remote candidates present in remote description to transport channels.
688 UseCandidatesInSessionDescription(remote_desc_.get());
689 return true;
690}
691
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000692void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
693 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694}
695
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000696cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
697 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698}
699
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000700bool WebRtcSession::GetSslRole(rtc::SSLRole* role) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000701 if (local_description() == NULL || remote_description() == NULL) {
702 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
703 << "SSL Role of the session.";
704 return false;
705 }
706
707 // TODO(mallinath) - Return role of each transport, as role may differ from
708 // one another.
709 // In current implementaion we just return the role of first transport in the
710 // transport map.
711 for (cricket::TransportMap::const_iterator iter = transport_proxies().begin();
712 iter != transport_proxies().end(); ++iter) {
713 if (iter->second->impl()) {
714 return iter->second->impl()->GetSslRole(role);
715 }
716 }
717 return false;
718}
719
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000720void WebRtcSession::CreateOffer(
721 CreateSessionDescriptionObserver* observer,
722 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
723 webrtc_session_desc_factory_->CreateOffer(observer, options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000724}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725
wu@webrtc.org91053e72013-08-10 07:18:04 +0000726void WebRtcSession::CreateAnswer(CreateSessionDescriptionObserver* observer,
727 const MediaConstraintsInterface* constraints) {
728 webrtc_session_desc_factory_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729}
730
731bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
732 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000733 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000734 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000735
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000736 // Validate SDP.
737 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
738 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000739 }
740
741 // Update the initiator flag if this session is the initiator.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000742 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743 if (state() == STATE_INIT && action == kOffer) {
744 set_initiator(true);
745 }
746
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000747 cricket::SecurePolicy sdes_policy =
748 webrtc_session_desc_factory_->SdesPolicy();
749 cricket::CryptoType crypto_required = dtls_enabled_ ?
750 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
751 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000752 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000753 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000754
755 set_local_description(desc->description()->Copy());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000756 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757
758 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000759 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760 // TODO(mallinath) - Handle CreateChannel failure, as new local description
761 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000762 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763 }
764
765 // Remove channel and transport proxies, if MediaContentDescription is
766 // rejected.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000767 RemoveUnusedChannelsAndTransports(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000769 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770 return false;
771 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000772
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 // Kick starting the ice candidates allocation.
774 StartCandidatesAllocation();
775
776 // Update state and SSRC of local MediaStreams and DataChannels based on the
777 // local session description.
778 mediastream_signaling_->OnLocalDescriptionChanged(local_desc_.get());
779
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000780 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000781 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
782 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
783 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000785 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786 }
787 return true;
788}
789
790bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
791 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000792 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000793 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000794
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000795 // Validate SDP.
796 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
797 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 }
799
800 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000801 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802 if (action == kOffer && !CreateChannels(desc->description())) {
803 // TODO(mallinath) - Handle CreateChannel failure, as new local description
804 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000805 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 }
807
808 // Remove channel and transport proxies, if MediaContentDescription is
809 // rejected.
810 RemoveUnusedChannelsAndTransports(desc->description());
811
812 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
813 // is called.
814 set_remote_description(desc->description()->Copy());
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000815 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 return false;
817 }
818
819 // Update remote MediaStreams.
820 mediastream_signaling_->OnRemoteDescriptionChanged(desc);
821 if (local_description() && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000822 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823 }
824
825 // Copy all saved candidates.
826 CopySavedCandidates(desc);
827 // We retain all received candidates.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000828 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
829 remote_desc_.get(), desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 // Check if this new SessionDescription contains new ice ufrag and password
831 // that indicates the remote peer requests ice restart.
832 ice_restart_latch_->CheckForRemoteIceRestart(remote_desc_.get(),
833 desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000834 remote_desc_.reset(desc_temp.release());
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000835
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000836 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000837 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
838 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
839 }
840
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000842 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000844
845 // Set the the ICE connection state to connecting since the connection may
846 // become writable with peer reflexive candidates before any remote candidate
847 // is signaled.
848 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
849 // is to have a new signal the indicates a change in checking state from the
850 // transport and expose a new checking() member from transport that can be
851 // read to determine the current checking state. The existing SignalConnecting
852 // actually means "gathering candidates", so cannot be be used here.
853 if (desc->type() != SessionDescriptionInterface::kOffer &&
854 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
855 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
856 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000857 return true;
858}
859
860bool WebRtcSession::UpdateSessionState(
861 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 std::string* err_desc) {
863 // If there's already a pending error then no state transition should happen.
864 // But all call-sites should be verifying this before calling us!
865 ASSERT(error() == cricket::BaseSession::ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000866 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000868 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
869 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870 }
871 SetState(source == cricket::CS_LOCAL ?
872 STATE_SENTINITIATE : STATE_RECEIVEDINITIATE);
873 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000874 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 }
876 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000877 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
878 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879 }
880 EnableChannels();
881 SetState(source == cricket::CS_LOCAL ?
882 STATE_SENTPRACCEPT : STATE_RECEIVEDPRACCEPT);
883 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000884 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 }
886 } else if (action == kAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000887 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
888 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889 }
890 MaybeEnableMuxingSupport();
891 EnableChannels();
892 SetState(source == cricket::CS_LOCAL ?
893 STATE_SENTACCEPT : STATE_RECEIVEDACCEPT);
894 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000895 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000896 }
897 }
898 return true;
899}
900
901WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
902 if (type == SessionDescriptionInterface::kOffer) {
903 return WebRtcSession::kOffer;
904 } else if (type == SessionDescriptionInterface::kPrAnswer) {
905 return WebRtcSession::kPrAnswer;
906 } else if (type == SessionDescriptionInterface::kAnswer) {
907 return WebRtcSession::kAnswer;
908 }
909 ASSERT(false && "unknown action type");
910 return WebRtcSession::kOffer;
911}
912
913bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
914 if (state() == STATE_INIT) {
915 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
916 << "without any offer (local or remote) "
917 << "session description.";
918 return false;
919 }
920
921 if (!candidate) {
922 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL";
923 return false;
924 }
925
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +0000926 bool valid = false;
927 if (!ReadyToUseRemoteCandidate(candidate, NULL, &valid)) {
928 if (valid) {
929 LOG(LS_INFO) << "ProcessIceMessage: Candidate saved";
930 saved_candidates_.push_back(
931 new JsepIceCandidate(candidate->sdp_mid(),
932 candidate->sdp_mline_index(),
933 candidate->candidate()));
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000934 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +0000935 return valid;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 }
937
938 // Add this candidate to the remote session description.
939 if (!remote_desc_->AddCandidate(candidate)) {
940 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used";
941 return false;
942 }
943
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000944 return UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945}
946
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000947bool WebRtcSession::SetIceTransports(
948 PeerConnectionInterface::IceTransportsType type) {
949 return port_allocator()->set_candidate_filter(
950 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000951}
952
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000953bool WebRtcSession::GetLocalTrackIdBySsrc(uint32 ssrc, std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954 if (!BaseSession::local_description())
955 return false;
956 return webrtc::GetTrackIdBySsrc(
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000957 BaseSession::local_description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000958}
959
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000960bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32 ssrc, std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000961 if (!BaseSession::remote_description())
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000962 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963 return webrtc::GetTrackIdBySsrc(
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000964 BaseSession::remote_description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965}
966
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000967std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000968 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000969 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970 return desc.str();
971}
972
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000973void WebRtcSession::SetAudioPlayout(uint32 ssrc, bool enable,
974 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000975 ASSERT(signaling_thread()->IsCurrent());
976 if (!voice_channel_) {
977 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
978 return;
979 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000980 if (!voice_channel_->SetRemoteRenderer(ssrc, renderer)) {
981 // SetRenderer() can fail if the ssrc does not match any playout channel.
982 LOG(LS_ERROR) << "SetAudioPlayout: ssrc is incorrect: " << ssrc;
983 return;
984 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000985 if (!voice_channel_->SetOutputScaling(ssrc, enable ? 1 : 0, enable ? 1 : 0)) {
986 // Allow that SetOutputScaling fail if |enable| is false but assert
987 // otherwise. This in the normal case when the underlying media channel has
988 // already been deleted.
989 ASSERT(enable == false);
990 }
991}
992
993void WebRtcSession::SetAudioSend(uint32 ssrc, bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000994 const cricket::AudioOptions& options,
995 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000996 ASSERT(signaling_thread()->IsCurrent());
997 if (!voice_channel_) {
998 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
999 return;
1000 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001001 if (!voice_channel_->SetLocalRenderer(ssrc, renderer)) {
1002 // SetRenderer() can fail if the ssrc does not match any send channel.
1003 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
1004 return;
1005 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006 if (!voice_channel_->MuteStream(ssrc, !enable)) {
1007 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1008 // This in the normal case when the underlying media channel has already
1009 // been deleted.
1010 ASSERT(enable == false);
1011 return;
1012 }
1013 if (enable)
1014 voice_channel_->SetChannelOptions(options);
1015}
1016
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001017void WebRtcSession::SetAudioPlayoutVolume(uint32 ssrc, double volume) {
1018 ASSERT(signaling_thread()->IsCurrent());
1019 ASSERT(volume >= 0 && volume <= 10);
1020 if (!voice_channel_) {
1021 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1022 return;
1023 }
1024
1025 if (!voice_channel_->SetOutputScaling(ssrc, volume, volume))
1026 ASSERT(false);
1027}
1028
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001029bool WebRtcSession::SetCaptureDevice(uint32 ssrc,
1030 cricket::VideoCapturer* camera) {
1031 ASSERT(signaling_thread()->IsCurrent());
1032
1033 if (!video_channel_.get()) {
1034 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1035 // support video.
1036 LOG(LS_WARNING) << "Video not used in this call.";
1037 return false;
1038 }
1039 if (!video_channel_->SetCapturer(ssrc, camera)) {
1040 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1041 // This in the normal case when the underlying media channel has already
1042 // been deleted.
1043 ASSERT(camera == NULL);
1044 return false;
1045 }
1046 return true;
1047}
1048
1049void WebRtcSession::SetVideoPlayout(uint32 ssrc,
1050 bool enable,
1051 cricket::VideoRenderer* renderer) {
1052 ASSERT(signaling_thread()->IsCurrent());
1053 if (!video_channel_) {
1054 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1055 return;
1056 }
1057 if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) {
1058 // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise.
1059 // This in the normal case when the underlying media channel has already
1060 // been deleted.
1061 ASSERT(renderer == NULL);
1062 }
1063}
1064
1065void WebRtcSession::SetVideoSend(uint32 ssrc, bool enable,
1066 const cricket::VideoOptions* options) {
1067 ASSERT(signaling_thread()->IsCurrent());
1068 if (!video_channel_) {
1069 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1070 return;
1071 }
1072 if (!video_channel_->MuteStream(ssrc, !enable)) {
1073 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1074 // This in the normal case when the underlying media channel has already
1075 // been deleted.
1076 ASSERT(enable == false);
1077 return;
1078 }
1079 if (enable && options)
1080 video_channel_->SetChannelOptions(*options);
1081}
1082
1083bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1084 ASSERT(signaling_thread()->IsCurrent());
1085 if (!voice_channel_) {
1086 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1087 return false;
1088 }
1089 uint32 send_ssrc = 0;
1090 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1091 // exists.
1092 if (!GetAudioSsrcByTrackId(BaseSession::local_description(), track_id,
1093 &send_ssrc)) {
1094 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1095 return false;
1096 }
1097 return voice_channel_->CanInsertDtmf();
1098}
1099
1100bool WebRtcSession::InsertDtmf(const std::string& track_id,
1101 int code, int duration) {
1102 ASSERT(signaling_thread()->IsCurrent());
1103 if (!voice_channel_) {
1104 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1105 return false;
1106 }
1107 uint32 send_ssrc = 0;
1108 if (!VERIFY(GetAudioSsrcByTrackId(BaseSession::local_description(),
1109 track_id, &send_ssrc))) {
1110 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1111 return false;
1112 }
1113 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration,
1114 cricket::DF_SEND)) {
1115 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1116 return false;
1117 }
1118 return true;
1119}
1120
1121sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
1122 return &SignalVoiceChannelDestroyed;
1123}
1124
wu@webrtc.org78187522013-10-07 23:32:02 +00001125bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001126 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001127 cricket::SendDataResult* result) {
1128 if (!data_channel_.get()) {
1129 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1130 return false;
1131 }
1132 return data_channel_->SendData(params, payload, result);
1133}
1134
1135bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
1136 if (!data_channel_.get()) {
1137 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1138 return false;
1139 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001140 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1141 &DataChannel::OnChannelReady);
1142 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1143 &DataChannel::OnDataReceived);
wu@webrtc.org78187522013-10-07 23:32:02 +00001144 return true;
1145}
1146
1147void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001148 if (!data_channel_.get()) {
1149 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1150 return;
1151 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001152 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1153 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
1154}
1155
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001156void WebRtcSession::AddSctpDataStream(int sid) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001157 if (!data_channel_.get()) {
1158 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1159 return;
1160 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001161 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1162 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001163}
1164
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001165void WebRtcSession::RemoveSctpDataStream(int sid) {
1166 mediastream_signaling_->RemoveSctpDataChannel(sid);
jiayl@webrtc.org2eaac182014-06-17 16:02:46 +00001167
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001168 if (!data_channel_.get()) {
1169 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1170 << "NULL.";
1171 return;
1172 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001173 data_channel_->RemoveRecvStream(sid);
1174 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001175}
1176
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001177bool WebRtcSession::ReadyToSendData() const {
1178 return data_channel_.get() && data_channel_->ready_to_send_data();
1179}
1180
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001181rtc::scoped_refptr<DataChannel> WebRtcSession::CreateDataChannel(
wu@webrtc.org78187522013-10-07 23:32:02 +00001182 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001183 const InternalDataChannelInit* config) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001184 if (state() == STATE_RECEIVEDTERMINATE) {
1185 return NULL;
1186 }
1187 if (data_channel_type_ == cricket::DCT_NONE) {
1188 LOG(LS_ERROR) << "CreateDataChannel: Data is not supported in this call.";
1189 return NULL;
1190 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001191 InternalDataChannelInit new_config =
1192 config ? (*config) : InternalDataChannelInit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001193 if (data_channel_type_ == cricket::DCT_SCTP) {
1194 if (new_config.id < 0) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001195 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001196 if (GetSslRole(&role) &&
1197 !mediastream_signaling_->AllocateSctpSid(role, &new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001198 LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel.";
1199 return NULL;
1200 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001201 } else if (!mediastream_signaling_->IsSctpSidAvailable(new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001202 LOG(LS_ERROR) << "Failed to create a SCTP data channel "
1203 << "because the id is already in use or out of range.";
1204 return NULL;
1205 }
1206 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001207
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001208 rtc::scoped_refptr<DataChannel> channel(DataChannel::Create(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001209 this, data_channel_type_, label, new_config));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001210 if (channel && !mediastream_signaling_->AddDataChannel(channel))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001211 return NULL;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001212
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001213 return channel;
1214}
1215
1216cricket::DataChannelType WebRtcSession::data_channel_type() const {
1217 return data_channel_type_;
1218}
1219
wu@webrtc.org91053e72013-08-10 07:18:04 +00001220bool WebRtcSession::IceRestartPending() const {
1221 return ice_restart_latch_->Get();
1222}
1223
1224void WebRtcSession::ResetIceRestartLatch() {
1225 ice_restart_latch_->Reset();
1226}
1227
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001228void WebRtcSession::OnIdentityReady(rtc::SSLIdentity* identity) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001229 SetIdentity(identity);
1230}
1231
1232bool WebRtcSession::waiting_for_identity() const {
1233 return webrtc_session_desc_factory_->waiting_for_identity();
1234}
1235
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001236void WebRtcSession::SetIceConnectionState(
1237 PeerConnectionInterface::IceConnectionState state) {
1238 if (ice_connection_state_ == state) {
1239 return;
1240 }
1241
1242 // ASSERT that the requested transition is allowed. Note that
1243 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1244 // within PeerConnection). This switch statement should compile away when
1245 // ASSERTs are disabled.
1246 switch (ice_connection_state_) {
1247 case PeerConnectionInterface::kIceConnectionNew:
1248 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1249 break;
1250 case PeerConnectionInterface::kIceConnectionChecking:
1251 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1252 state == PeerConnectionInterface::kIceConnectionConnected);
1253 break;
1254 case PeerConnectionInterface::kIceConnectionConnected:
1255 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1256 state == PeerConnectionInterface::kIceConnectionChecking ||
1257 state == PeerConnectionInterface::kIceConnectionCompleted);
1258 break;
1259 case PeerConnectionInterface::kIceConnectionCompleted:
1260 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1261 state == PeerConnectionInterface::kIceConnectionDisconnected);
1262 break;
1263 case PeerConnectionInterface::kIceConnectionFailed:
1264 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1265 break;
1266 case PeerConnectionInterface::kIceConnectionDisconnected:
1267 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1268 state == PeerConnectionInterface::kIceConnectionConnected ||
1269 state == PeerConnectionInterface::kIceConnectionCompleted ||
1270 state == PeerConnectionInterface::kIceConnectionFailed);
1271 break;
1272 case PeerConnectionInterface::kIceConnectionClosed:
1273 ASSERT(false);
1274 break;
1275 default:
1276 ASSERT(false);
1277 break;
1278 }
1279
1280 ice_connection_state_ = state;
1281 if (ice_observer_) {
1282 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1283 }
1284}
1285
1286void WebRtcSession::OnTransportRequestSignaling(
1287 cricket::Transport* transport) {
1288 ASSERT(signaling_thread()->IsCurrent());
1289 transport->OnSignalingReady();
1290 if (ice_observer_) {
1291 ice_observer_->OnIceGatheringChange(
1292 PeerConnectionInterface::kIceGatheringGathering);
1293 }
1294}
1295
1296void WebRtcSession::OnTransportConnecting(cricket::Transport* transport) {
1297 ASSERT(signaling_thread()->IsCurrent());
1298 // start monitoring for the write state of the transport.
1299 OnTransportWritable(transport);
1300}
1301
1302void WebRtcSession::OnTransportWritable(cricket::Transport* transport) {
1303 ASSERT(signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304 if (transport->all_channels_writable()) {
henrike@webrtc.org05376342014-03-10 15:53:12 +00001305 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001306 } else if (transport->HasChannels()) {
1307 // If the current state is Connected or Completed, then there were writable
1308 // channels but now there are not, so the next state must be Disconnected.
1309 if (ice_connection_state_ ==
1310 PeerConnectionInterface::kIceConnectionConnected ||
1311 ice_connection_state_ ==
1312 PeerConnectionInterface::kIceConnectionCompleted) {
1313 SetIceConnectionState(
1314 PeerConnectionInterface::kIceConnectionDisconnected);
1315 }
1316 }
1317}
1318
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001319void WebRtcSession::OnTransportCompleted(cricket::Transport* transport) {
1320 ASSERT(signaling_thread()->IsCurrent());
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001321 PeerConnectionInterface::IceConnectionState old_state = ice_connection_state_;
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001322 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001323 // Only report once when Ice connection is completed.
1324 if (old_state != PeerConnectionInterface::kIceConnectionCompleted) {
1325 ReportBestConnectionState(transport);
1326 }
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001327}
1328
1329void WebRtcSession::OnTransportFailed(cricket::Transport* transport) {
1330 ASSERT(signaling_thread()->IsCurrent());
1331 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1332}
1333
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001334void WebRtcSession::OnTransportProxyCandidatesReady(
1335 cricket::TransportProxy* proxy, const cricket::Candidates& candidates) {
1336 ASSERT(signaling_thread()->IsCurrent());
1337 ProcessNewLocalCandidate(proxy->content_name(), candidates);
1338}
1339
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340void WebRtcSession::OnCandidatesAllocationDone() {
1341 ASSERT(signaling_thread()->IsCurrent());
1342 if (ice_observer_) {
1343 ice_observer_->OnIceGatheringChange(
1344 PeerConnectionInterface::kIceGatheringComplete);
1345 ice_observer_->OnIceComplete();
1346 }
1347}
1348
1349// Enabling voice and video channel.
1350void WebRtcSession::EnableChannels() {
1351 if (voice_channel_ && !voice_channel_->enabled())
1352 voice_channel_->Enable(true);
1353
1354 if (video_channel_ && !video_channel_->enabled())
1355 video_channel_->Enable(true);
1356
1357 if (data_channel_.get() && !data_channel_->enabled())
1358 data_channel_->Enable(true);
1359}
1360
1361void WebRtcSession::ProcessNewLocalCandidate(
1362 const std::string& content_name,
1363 const cricket::Candidates& candidates) {
1364 int sdp_mline_index;
1365 if (!GetLocalCandidateMediaIndex(content_name, &sdp_mline_index)) {
1366 LOG(LS_ERROR) << "ProcessNewLocalCandidate: content name "
1367 << content_name << " not found";
1368 return;
1369 }
1370
1371 for (cricket::Candidates::const_iterator citer = candidates.begin();
1372 citer != candidates.end(); ++citer) {
1373 // Use content_name as the candidate media id.
1374 JsepIceCandidate candidate(content_name, sdp_mline_index, *citer);
1375 if (ice_observer_) {
1376 ice_observer_->OnIceCandidate(&candidate);
1377 }
1378 if (local_desc_) {
1379 local_desc_->AddCandidate(&candidate);
1380 }
1381 }
1382}
1383
1384// Returns the media index for a local ice candidate given the content name.
1385bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1386 int* sdp_mline_index) {
1387 if (!BaseSession::local_description() || !sdp_mline_index)
1388 return false;
1389
1390 bool content_found = false;
1391 const ContentInfos& contents = BaseSession::local_description()->contents();
1392 for (size_t index = 0; index < contents.size(); ++index) {
1393 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001394 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001395 content_found = true;
1396 break;
1397 }
1398 }
1399 return content_found;
1400}
1401
1402bool WebRtcSession::UseCandidatesInSessionDescription(
1403 const SessionDescriptionInterface* remote_desc) {
1404 if (!remote_desc)
1405 return true;
1406 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001407
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001408 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1409 const IceCandidateCollection* candidates = remote_desc->candidates(m);
1410 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001411 const IceCandidateInterface* candidate = candidates->at(n);
1412 bool valid = false;
1413 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1414 if (valid) {
1415 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Candidate saved.";
1416 saved_candidates_.push_back(
1417 new JsepIceCandidate(candidate->sdp_mid(),
1418 candidate->sdp_mline_index(),
1419 candidate->candidate()));
1420 }
1421 continue;
1422 }
1423
1424 ret = UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001425 if (!ret)
1426 break;
1427 }
1428 }
1429 return ret;
1430}
1431
1432bool WebRtcSession::UseCandidate(
1433 const IceCandidateInterface* candidate) {
1434
1435 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
1436 size_t remote_content_size =
1437 BaseSession::remote_description()->contents().size();
1438 if (mediacontent_index >= remote_content_size) {
1439 LOG(LS_ERROR)
1440 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1441 return false;
1442 }
1443
1444 cricket::ContentInfo content =
1445 BaseSession::remote_description()->contents()[mediacontent_index];
1446 std::vector<cricket::Candidate> candidates;
1447 candidates.push_back(candidate->candidate());
1448 // Invoking BaseSession method to handle remote candidates.
1449 std::string error;
1450 if (OnRemoteCandidates(content.name, candidates, &error)) {
1451 // Candidates successfully submitted for checking.
1452 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1453 ice_connection_state_ ==
1454 PeerConnectionInterface::kIceConnectionDisconnected) {
1455 // If state is New, then the session has just gotten its first remote ICE
1456 // candidates, so go to Checking.
1457 // If state is Disconnected, the session is re-using old candidates or
1458 // receiving additional ones, so go to Checking.
1459 // If state is Connected, stay Connected.
1460 // TODO(bemasc): If state is Connected, and the new candidates are for a
1461 // newly added transport, then the state actually _should_ move to
1462 // checking. Add a way to distinguish that case.
1463 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1464 }
1465 // TODO(bemasc): If state is Completed, go back to Connected.
1466 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001467 if (!error.empty()) {
1468 LOG(LS_WARNING) << error;
1469 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001470 }
1471 return true;
1472}
1473
1474void WebRtcSession::RemoveUnusedChannelsAndTransports(
1475 const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001476 // Destroy video_channel_ first since it may have a pointer to the
1477 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001478 const cricket::ContentInfo* video_info =
1479 cricket::GetFirstVideoContent(desc);
1480 if ((!video_info || video_info->rejected) && video_channel_) {
1481 mediastream_signaling_->OnVideoChannelClose();
1482 SignalVideoChannelDestroyed();
1483 const std::string content_name = video_channel_->content_name();
1484 channel_manager_->DestroyVideoChannel(video_channel_.release());
bjornv@webrtc.org520a69e2015-02-04 12:45:44 +00001485 DestroyTransportProxy(content_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001486 }
1487
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001488 const cricket::ContentInfo* voice_info =
1489 cricket::GetFirstAudioContent(desc);
1490 if ((!voice_info || voice_info->rejected) && voice_channel_) {
1491 mediastream_signaling_->OnAudioChannelClose();
1492 SignalVoiceChannelDestroyed();
1493 const std::string content_name = voice_channel_->content_name();
1494 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
bjornv@webrtc.org520a69e2015-02-04 12:45:44 +00001495 DestroyTransportProxy(content_name);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001496 }
1497
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001498 const cricket::ContentInfo* data_info =
1499 cricket::GetFirstDataContent(desc);
1500 if ((!data_info || data_info->rejected) && data_channel_) {
1501 mediastream_signaling_->OnDataChannelClose();
1502 SignalDataChannelDestroyed();
1503 const std::string content_name = data_channel_->content_name();
1504 channel_manager_->DestroyDataChannel(data_channel_.release());
bjornv@webrtc.org520a69e2015-02-04 12:45:44 +00001505 DestroyTransportProxy(content_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001506 }
1507}
1508
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001509// TODO(mallinath) - Add a correct error code if the channels are not creatued
1510// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001511bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
1512 // Disabling the BUNDLE flag in PortAllocator if offer disabled it.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001513 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1514 if (state() == STATE_INIT && !bundle_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001515 port_allocator()->set_flags(port_allocator()->flags() &
1516 ~cricket::PORTALLOCATOR_ENABLE_BUNDLE);
1517 }
1518
1519 // Creating the media channels and transport proxies.
1520 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1521 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001522 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001523 LOG(LS_ERROR) << "Failed to create voice channel.";
1524 return false;
1525 }
1526 }
1527
1528 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1529 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001530 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001531 LOG(LS_ERROR) << "Failed to create video channel.";
1532 return false;
1533 }
1534 }
1535
1536 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1537 if (data_channel_type_ != cricket::DCT_NONE &&
1538 data && !data->rejected && !data_channel_.get()) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001539 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001540 LOG(LS_ERROR) << "Failed to create data channel.";
1541 return false;
1542 }
1543 }
1544
1545 return true;
1546}
1547
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001548bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001549 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001550 this, content->name, true));
wu@webrtc.orgde305012013-10-31 15:40:38 +00001551 if (!voice_channel_.get())
1552 return false;
1553
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00001554 voice_channel_->SetChannelOptions(audio_options_);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001555 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001556}
1557
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001558bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001559 video_channel_.reset(channel_manager_->CreateVideoChannel(
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +00001560 this, content->name, true, video_options_, voice_channel_.get()));
1561 return video_channel_.get() != NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001562}
1563
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001564bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001565 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001566 data_channel_.reset(channel_manager_->CreateDataChannel(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001567 this, content->name, !sctp, data_channel_type_));
wu@webrtc.org91053e72013-08-10 07:18:04 +00001568 if (!data_channel_.get()) {
1569 return false;
1570 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001571 if (sctp) {
1572 mediastream_signaling_->OnDataTransportCreatedForSctp();
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001573 data_channel_->SignalDataReceived.connect(
1574 this, &WebRtcSession::OnDataChannelMessageReceived);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00001575 data_channel_->SignalStreamClosedRemotely.connect(
1576 mediastream_signaling_,
1577 &MediaStreamSignaling::OnRemoteSctpDataChannelClosed);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001578 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001579 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001580}
1581
1582void WebRtcSession::CopySavedCandidates(
1583 SessionDescriptionInterface* dest_desc) {
1584 if (!dest_desc) {
1585 ASSERT(false);
1586 return;
1587 }
1588 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
1589 dest_desc->AddCandidate(saved_candidates_[i]);
1590 delete saved_candidates_[i];
1591 }
1592 saved_candidates_.clear();
1593}
1594
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001595void WebRtcSession::OnDataChannelMessageReceived(
1596 cricket::DataChannel* channel,
1597 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001598 const rtc::Buffer& payload) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001599 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001600 if (params.type == cricket::DMT_CONTROL &&
1601 mediastream_signaling_->IsSctpSidAvailable(params.ssrc)) {
1602 // Received CONTROL on unused sid, process as an OPEN message.
1603 mediastream_signaling_->AddDataChannelFromOpenMessage(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001604 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001605 // otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001606}
1607
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001608// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001609bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001610 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1611 if (!bundle_enabled)
1612 return true;
1613
1614 const cricket::ContentGroup* bundle_group =
1615 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1616 ASSERT(bundle_group != NULL);
1617
1618 const cricket::ContentInfos& contents = desc->contents();
1619 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1620 citer != contents.end(); ++citer) {
1621 const cricket::ContentInfo* content = (&*citer);
1622 ASSERT(content != NULL);
1623 if (bundle_group->HasContentName(content->name) &&
1624 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1625 if (!HasRtcpMuxEnabled(content))
1626 return false;
1627 }
1628 }
1629 // RTCP-MUX is enabled in all the contents.
1630 return true;
1631}
1632
1633bool WebRtcSession::HasRtcpMuxEnabled(
1634 const cricket::ContentInfo* content) {
1635 const cricket::MediaContentDescription* description =
1636 static_cast<cricket::MediaContentDescription*>(content->description);
1637 return description->rtcp_mux();
1638}
1639
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001640bool WebRtcSession::ValidateSessionDescription(
1641 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001642 cricket::ContentSource source, std::string* err_desc) {
1643 std::string type;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001644 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001645 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001646 }
1647
1648 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001649 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001650 }
1651
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001652 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001653 Action action = GetAction(sdesc->type());
1654 if (source == cricket::CS_LOCAL) {
1655 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001656 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001657 } else {
1658 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001659 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001660 }
1661
1662 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001663 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001664 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1665 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001666 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001667 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001668 }
1669
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001670 // Verify ice-ufrag and ice-pwd.
1671 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001672 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001673 }
1674
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001675 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001676 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001677 }
1678
1679 // Verify m-lines in Answer when compared against Offer.
1680 if (action == kAnswer) {
1681 const cricket::SessionDescription* offer_desc =
1682 (source == cricket::CS_LOCAL) ? remote_description()->description() :
1683 local_description()->description();
1684 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001685 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001686 }
1687 }
1688
1689 return true;
1690}
1691
1692bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1693 return ((action == kOffer && state() == STATE_INIT) ||
1694 // update local offer
1695 (action == kOffer && state() == STATE_SENTINITIATE) ||
1696 // update the current ongoing session.
1697 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1698 (action == kOffer && state() == STATE_SENTACCEPT) ||
1699 (action == kOffer && state() == STATE_INPROGRESS) ||
1700 // accept remote offer
1701 (action == kAnswer && state() == STATE_RECEIVEDINITIATE) ||
1702 (action == kAnswer && state() == STATE_SENTPRACCEPT) ||
1703 (action == kPrAnswer && state() == STATE_RECEIVEDINITIATE) ||
1704 (action == kPrAnswer && state() == STATE_SENTPRACCEPT));
1705}
1706
1707bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1708 return ((action == kOffer && state() == STATE_INIT) ||
1709 // update remote offer
1710 (action == kOffer && state() == STATE_RECEIVEDINITIATE) ||
1711 // update the current ongoing session
1712 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1713 (action == kOffer && state() == STATE_SENTACCEPT) ||
1714 (action == kOffer && state() == STATE_INPROGRESS) ||
1715 // accept local offer
1716 (action == kAnswer && state() == STATE_SENTINITIATE) ||
1717 (action == kAnswer && state() == STATE_RECEIVEDPRACCEPT) ||
1718 (action == kPrAnswer && state() == STATE_SENTINITIATE) ||
1719 (action == kPrAnswer && state() == STATE_RECEIVEDPRACCEPT));
1720}
1721
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001722std::string WebRtcSession::GetSessionErrorMsg() {
1723 std::ostringstream desc;
1724 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1725 desc << kSessionErrorDesc << error_desc() << ".";
1726 return desc.str();
1727}
1728
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001729// We need to check the local/remote description for the Transport instead of
1730// the session, because a new Transport added during renegotiation may have
1731// them unset while the session has them set from the previous negotiation.
1732// Not doing so may trigger the auto generation of transport description and
1733// mess up DTLS identity information, ICE credential, etc.
1734bool WebRtcSession::ReadyToUseRemoteCandidate(
1735 const IceCandidateInterface* candidate,
1736 const SessionDescriptionInterface* remote_desc,
1737 bool* valid) {
1738 *valid = true;;
1739 cricket::TransportProxy* transport_proxy = NULL;
1740
1741 const SessionDescriptionInterface* current_remote_desc =
1742 remote_desc ? remote_desc : remote_description();
1743
1744 if (!current_remote_desc)
1745 return false;
1746
1747 size_t mediacontent_index =
1748 static_cast<size_t>(candidate->sdp_mline_index());
1749 size_t remote_content_size =
1750 current_remote_desc->description()->contents().size();
1751 if (mediacontent_index >= remote_content_size) {
1752 LOG(LS_ERROR)
1753 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
1754
1755 *valid = false;
1756 return false;
1757 }
1758
1759 cricket::ContentInfo content =
1760 current_remote_desc->description()->contents()[mediacontent_index];
1761 transport_proxy = GetTransportProxy(content.name);
1762
1763 return transport_proxy && transport_proxy->local_description_set() &&
1764 transport_proxy->remote_description_set();
1765}
1766
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001767// Walk through the ConnectionInfos to gather best connection usage
1768// for IPv4 and IPv6.
1769void WebRtcSession::ReportBestConnectionState(cricket::Transport* transport) {
1770 if (!metrics_observer_) {
1771 return;
1772 }
1773
1774 cricket::TransportStats stats;
1775 if (!transport->GetStats(&stats)) {
1776 return;
1777 }
1778
1779 for (cricket::TransportChannelStatsList::const_iterator it =
1780 stats.channel_stats.begin();
1781 it != stats.channel_stats.end(); ++it) {
1782 for (cricket::ConnectionInfos::const_iterator it_info =
1783 it->connection_infos.begin();
1784 it_info != it->connection_infos.end(); ++it_info) {
1785 if (!it_info->best_connection) {
1786 continue;
1787 }
1788 if (it_info->local_candidate.address().family() == AF_INET) {
1789 metrics_observer_->IncrementCounter(kBestConnections_IPv4);
1790 } else if (it_info->local_candidate.address().family() ==
1791 AF_INET6) {
1792 metrics_observer_->IncrementCounter(kBestConnections_IPv6);
1793 } else {
1794 ASSERT(false);
1795 }
1796 return;
1797 }
1798 }
1799}
1800
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001801} // namespace webrtc