blob: 81e6c0fbd52eec6e2a8e6a46abe2010c90772555 [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);
212 cricket::StreamParams stream;
213 if (!cricket::GetStreamByIds(audio_content->streams(), "", track_id,
214 &stream)) {
215 return false;
216 }
217 *ssrc = stream.first_ssrc();
218 return true;
219}
220
221static bool GetTrackIdBySsrc(const SessionDescription* session_description,
222 uint32 ssrc, std::string* track_id) {
223 ASSERT(track_id != NULL);
224
225 cricket::StreamParams stream_out;
226 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
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000233 if (cricket::GetStreamBySsrc(audio_content->streams(), ssrc, &stream_out)) {
234 *track_id = stream_out.id;
235 return true;
236 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000237 }
238
239 const cricket::ContentInfo* video_info =
240 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000241 if (video_info) {
242 const cricket::MediaContentDescription* video_content =
243 static_cast<const cricket::MediaContentDescription*>(
244 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000246 if (cricket::GetStreamBySsrc(video_content->streams(), ssrc, &stream_out)) {
247 *track_id = stream_out.id;
248 return true;
249 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250 }
251 return false;
252}
253
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000254static bool BadSdp(const std::string& source,
255 const std::string& type,
256 const std::string& reason,
257 std::string* err_desc) {
258 std::ostringstream desc;
259 desc << "Failed to set " << source << " " << type << " sdp: " << reason;
260
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000262 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000264 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265 return false;
266}
267
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000269 const std::string& type,
270 const std::string& reason,
271 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000273 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000275 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276 }
277}
278
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000279static bool BadLocalSdp(const std::string& type,
280 const std::string& reason,
281 std::string* err_desc) {
282 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
283}
284
285static bool BadRemoteSdp(const std::string& type,
286 const std::string& reason,
287 std::string* err_desc) {
288 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
289}
290
291static bool BadOfferSdp(cricket::ContentSource source,
292 const std::string& reason,
293 std::string* err_desc) {
294 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
295}
296
297static bool BadPranswerSdp(cricket::ContentSource source,
298 const std::string& reason,
299 std::string* err_desc) {
300 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
301 reason, err_desc);
302}
303
304static bool BadAnswerSdp(cricket::ContentSource source,
305 const std::string& reason,
306 std::string* err_desc) {
307 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308}
309
310#define GET_STRING_OF_STATE(state) \
311 case cricket::BaseSession::state: \
312 result = #state; \
313 break;
314
315static std::string GetStateString(cricket::BaseSession::State state) {
316 std::string result;
317 switch (state) {
318 GET_STRING_OF_STATE(STATE_INIT)
319 GET_STRING_OF_STATE(STATE_SENTINITIATE)
320 GET_STRING_OF_STATE(STATE_RECEIVEDINITIATE)
321 GET_STRING_OF_STATE(STATE_SENTPRACCEPT)
322 GET_STRING_OF_STATE(STATE_SENTACCEPT)
323 GET_STRING_OF_STATE(STATE_RECEIVEDPRACCEPT)
324 GET_STRING_OF_STATE(STATE_RECEIVEDACCEPT)
325 GET_STRING_OF_STATE(STATE_SENTMODIFY)
326 GET_STRING_OF_STATE(STATE_RECEIVEDMODIFY)
327 GET_STRING_OF_STATE(STATE_SENTREJECT)
328 GET_STRING_OF_STATE(STATE_RECEIVEDREJECT)
329 GET_STRING_OF_STATE(STATE_SENTREDIRECT)
330 GET_STRING_OF_STATE(STATE_SENTTERMINATE)
331 GET_STRING_OF_STATE(STATE_RECEIVEDTERMINATE)
332 GET_STRING_OF_STATE(STATE_INPROGRESS)
333 GET_STRING_OF_STATE(STATE_DEINIT)
334 default:
335 ASSERT(false);
336 break;
337 }
338 return result;
339}
340
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000341#define GET_STRING_OF_ERROR_CODE(err) \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342 case cricket::BaseSession::err: \
343 result = #err; \
344 break;
345
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000346static std::string GetErrorCodeString(cricket::BaseSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347 std::string result;
348 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000349 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
350 GET_STRING_OF_ERROR_CODE(ERROR_TIME)
351 GET_STRING_OF_ERROR_CODE(ERROR_RESPONSE)
352 GET_STRING_OF_ERROR_CODE(ERROR_NETWORK)
353 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
354 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355 default:
356 ASSERT(false);
357 break;
358 }
359 return result;
360}
361
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000362static std::string MakeErrorString(const std::string& error,
363 const std::string& desc) {
364 std::ostringstream ret;
365 ret << error << " " << desc;
366 return ret.str();
367}
368
369static std::string MakeTdErrorString(const std::string& desc) {
370 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371}
372
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000373// Set |option| to the highest-priority value of |key| in the optional
374// constraints if the key is found and has a valid value.
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000375template<typename T>
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000376static void SetOptionFromOptionalConstraint(
377 const MediaConstraintsInterface* constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000378 const std::string& key, cricket::Settable<T>* option) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000379 if (!constraints) {
380 return;
381 }
382 std::string string_value;
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000383 T value;
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000384 if (constraints->GetOptional().FindFirst(key, &string_value)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000385 if (rtc::FromString(string_value, &value)) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000386 option->Set(value);
387 }
388 }
389}
390
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000391uint32 ConvertIceTransportTypeToCandidateFilter(
392 PeerConnectionInterface::IceTransportsType type) {
393 switch (type) {
394 case PeerConnectionInterface::kNone:
395 return cricket::CF_NONE;
396 case PeerConnectionInterface::kRelay:
397 return cricket::CF_RELAY;
398 case PeerConnectionInterface::kNoHost:
399 return (cricket::CF_ALL & ~cricket::CF_HOST);
400 case PeerConnectionInterface::kAll:
401 return cricket::CF_ALL;
402 default: ASSERT(false);
403 }
404 return cricket::CF_NONE;
405}
406
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407// Help class used to remember if a a remote peer has requested ice restart by
408// by sending a description with new ice ufrag and password.
409class IceRestartAnswerLatch {
410 public:
411 IceRestartAnswerLatch() : ice_restart_(false) { }
412
wu@webrtc.org91053e72013-08-10 07:18:04 +0000413 // Returns true if CheckForRemoteIceRestart has been called with a new session
414 // description where ice password and ufrag has changed since last time
415 // Reset() was called.
416 bool Get() const {
417 return ice_restart_;
418 }
419
420 void Reset() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000421 if (ice_restart_) {
422 ice_restart_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000423 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000424 }
425
426 void CheckForRemoteIceRestart(
427 const SessionDescriptionInterface* old_desc,
428 const SessionDescriptionInterface* new_desc) {
429 if (!old_desc || new_desc->type() != SessionDescriptionInterface::kOffer) {
430 return;
431 }
432 const SessionDescription* new_sd = new_desc->description();
433 const SessionDescription* old_sd = old_desc->description();
434 const ContentInfos& contents = new_sd->contents();
435 for (size_t index = 0; index < contents.size(); ++index) {
436 const ContentInfo* cinfo = &contents[index];
437 if (cinfo->rejected) {
438 continue;
439 }
440 // If the content isn't rejected, check if ufrag and password has
441 // changed.
442 const cricket::TransportDescription* new_transport_desc =
443 new_sd->GetTransportDescriptionByName(cinfo->name);
444 const cricket::TransportDescription* old_transport_desc =
445 old_sd->GetTransportDescriptionByName(cinfo->name);
446 if (!new_transport_desc || !old_transport_desc) {
447 // No transport description exist. This is not an ice restart.
448 continue;
449 }
jiayl@webrtc.orgdb397e52014-06-20 16:32:09 +0000450 if (cricket::IceCredentialsChanged(old_transport_desc->ice_ufrag,
451 old_transport_desc->ice_pwd,
452 new_transport_desc->ice_ufrag,
453 new_transport_desc->ice_pwd)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000454 LOG(LS_INFO) << "Remote peer request ice restart.";
455 ice_restart_ = true;
456 break;
457 }
458 }
459 }
460
461 private:
462 bool ice_restart_;
463};
464
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000465WebRtcSession::WebRtcSession(cricket::ChannelManager* channel_manager,
466 rtc::Thread* signaling_thread,
467 rtc::Thread* worker_thread,
468 cricket::PortAllocator* port_allocator,
469 MediaStreamSignaling* mediastream_signaling)
470 : cricket::BaseSession(signaling_thread,
471 worker_thread,
472 port_allocator,
473 rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX),
474 cricket::NS_JINGLE_RTP,
475 false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476 // RFC 3264: The numeric value of the session id and version in the
477 // o line MUST be representable with a "64 bit signed integer".
478 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
479 channel_manager_(channel_manager),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480 mediastream_signaling_(mediastream_signaling),
481 ice_observer_(NULL),
482 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000484 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000486 ice_restart_latch_(new IceRestartAnswerLatch),
487 metrics_observer_(NULL) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000488}
489
490WebRtcSession::~WebRtcSession() {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000491 // Destroy video_channel_ first since it may have a pointer to the
492 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493 if (video_channel_.get()) {
494 SignalVideoChannelDestroyed();
495 channel_manager_->DestroyVideoChannel(video_channel_.release());
496 }
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000497 if (voice_channel_.get()) {
498 SignalVoiceChannelDestroyed();
499 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
500 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501 if (data_channel_.get()) {
502 SignalDataChannelDestroyed();
503 channel_manager_->DestroyDataChannel(data_channel_.release());
504 }
505 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
506 delete saved_candidates_[i];
507 }
508 delete identity();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000509}
510
wu@webrtc.org91053e72013-08-10 07:18:04 +0000511bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000512 const PeerConnectionFactoryInterface::Options& options,
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000513 const MediaConstraintsInterface* constraints,
514 DTLSIdentityServiceInterface* dtls_identity_service,
515 PeerConnectionInterface::IceTransportsType ice_transport) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000516 // TODO(perkj): Take |constraints| into consideration. Return false if not all
517 // mandatory constraints can be fulfilled. Note that |constraints|
518 // can be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519 bool value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000520
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000521 if (options.disable_encryption) {
522 dtls_enabled_ = false;
523 } else {
524 // Enable DTLS by default if |dtls_identity_service| is valid.
525 dtls_enabled_ = (dtls_identity_service != NULL);
526 // |constraints| can override the default |dtls_enabled_| value.
527 if (FindConstraint(
528 constraints,
529 MediaConstraintsInterface::kEnableDtlsSrtp,
530 &value, NULL)) {
531 dtls_enabled_ = value;
532 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000533 }
534
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000536 // It takes precendence over the disable_sctp_data_channels
537 // PeerConnectionFactoryInterface::Options.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538 if (FindConstraint(
539 constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
540 &value, NULL) && value) {
541 LOG(LS_INFO) << "Allowing RTP data engine.";
542 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000543 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000544 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000545 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000546 LOG(LS_INFO) << "Allowing SCTP data engine.";
547 data_channel_type_ = cricket::DCT_SCTP;
548 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000549 }
550 if (data_channel_type_ != cricket::DCT_NONE) {
551 mediastream_signaling_->SetDataChannelFactory(this);
552 }
553
wu@webrtc.orgde305012013-10-31 15:40:38 +0000554 // Find DSCP constraint.
555 if (FindConstraint(
556 constraints,
557 MediaConstraintsInterface::kEnableDscp,
558 &value, NULL)) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000559 audio_options_.dscp.Set(value);
560 video_options_.dscp.Set(value);
561 }
562
563 // Find Suspend Below Min Bitrate constraint.
564 if (FindConstraint(
565 constraints,
566 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
567 &value,
568 NULL)) {
569 video_options_.suspend_below_min_bitrate.Set(value);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000570 }
571
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000572 SetOptionFromOptionalConstraint(constraints,
573 MediaConstraintsInterface::kScreencastMinBitrate,
574 &video_options_.screencast_min_bitrate);
575
576 // Find constraints for cpu overuse detection.
577 SetOptionFromOptionalConstraint(constraints,
578 MediaConstraintsInterface::kCpuUnderuseThreshold,
579 &video_options_.cpu_underuse_threshold);
580 SetOptionFromOptionalConstraint(constraints,
581 MediaConstraintsInterface::kCpuOveruseThreshold,
582 &video_options_.cpu_overuse_threshold);
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000583 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000584 MediaConstraintsInterface::kCpuOveruseDetection,
585 &video_options_.cpu_overuse_detection);
586 SetOptionFromOptionalConstraint(constraints,
587 MediaConstraintsInterface::kCpuOveruseEncodeUsage,
588 &video_options_.cpu_overuse_encode_usage);
589 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000590 MediaConstraintsInterface::kCpuUnderuseEncodeRsdThreshold,
591 &video_options_.cpu_underuse_encode_rsd_threshold);
592 SetOptionFromOptionalConstraint(constraints,
593 MediaConstraintsInterface::kCpuOveruseEncodeRsdThreshold,
594 &video_options_.cpu_overuse_encode_rsd_threshold);
buildbot@webrtc.orgdb563902014-06-13 13:05:48 +0000595
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000596 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000597 MediaConstraintsInterface::kNumUnsignalledRecvStreams,
598 &video_options_.unsignalled_recv_stream_limit);
599 if (video_options_.unsignalled_recv_stream_limit.IsSet()) {
600 int stream_limit;
601 video_options_.unsignalled_recv_stream_limit.Get(&stream_limit);
602 stream_limit = rtc::_min(kMaxUnsignalledRecvStreams, stream_limit);
603 stream_limit = rtc::_max(0, stream_limit);
604 video_options_.unsignalled_recv_stream_limit.Set(stream_limit);
605 }
606
607 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000608 MediaConstraintsInterface::kHighStartBitrate,
609 &video_options_.video_start_bitrate);
wu@webrtc.orgcfe5e9c2014-03-27 17:03:58 +0000610
611 if (FindConstraint(
612 constraints,
613 MediaConstraintsInterface::kVeryHighBitrate,
614 &value,
615 NULL)) {
616 video_options_.video_highest_bitrate.Set(
617 cricket::VideoOptions::VERY_HIGH);
618 } else if (FindConstraint(
619 constraints,
620 MediaConstraintsInterface::kHighBitrate,
621 &value,
622 NULL)) {
623 video_options_.video_highest_bitrate.Set(
624 cricket::VideoOptions::HIGH);
625 }
626
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000627 SetOptionFromOptionalConstraint(constraints,
628 MediaConstraintsInterface::kCombinedAudioVideoBwe,
629 &audio_options_.combined_audio_video_bwe);
630
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631 const cricket::VideoCodec default_codec(
632 JsepSessionDescription::kDefaultVideoCodecId,
633 JsepSessionDescription::kDefaultVideoCodecName,
634 JsepSessionDescription::kMaxVideoCodecWidth,
635 JsepSessionDescription::kMaxVideoCodecHeight,
636 JsepSessionDescription::kDefaultVideoCodecFramerate,
637 JsepSessionDescription::kDefaultVideoCodecPreference);
638 channel_manager_->SetDefaultVideoEncoderConfig(
639 cricket::VideoEncoderConfig(default_codec));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000640
641 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
642 signaling_thread(),
643 channel_manager_,
644 mediastream_signaling_,
645 dtls_identity_service,
646 this,
647 id(),
648 data_channel_type_,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000649 dtls_enabled_));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000650
651 webrtc_session_desc_factory_->SignalIdentityReady.connect(
652 this, &WebRtcSession::OnIdentityReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000653
wu@webrtc.org97077a32013-10-25 21:18:33 +0000654 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000655 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000656 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000657 port_allocator()->set_candidate_filter(
658 ConvertIceTransportTypeToCandidateFilter(ice_transport));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 return true;
660}
661
662void WebRtcSession::Terminate() {
663 SetState(STATE_RECEIVEDTERMINATE);
664 RemoveUnusedChannelsAndTransports(NULL);
665 ASSERT(voice_channel_.get() == NULL);
666 ASSERT(video_channel_.get() == NULL);
667 ASSERT(data_channel_.get() == NULL);
668}
669
670bool WebRtcSession::StartCandidatesAllocation() {
671 // SpeculativelyConnectTransportChannels, will call ConnectChannels method
672 // from TransportProxy to start gathering ice candidates.
673 SpeculativelyConnectAllTransportChannels();
674 if (!saved_candidates_.empty()) {
675 // If there are saved candidates which arrived before local description is
676 // set, copy those to remote description.
677 CopySavedCandidates(remote_desc_.get());
678 }
679 // Push remote candidates present in remote description to transport channels.
680 UseCandidatesInSessionDescription(remote_desc_.get());
681 return true;
682}
683
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000684void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
685 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686}
687
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000688cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
689 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000690}
691
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000692bool WebRtcSession::GetSslRole(rtc::SSLRole* role) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000693 if (local_description() == NULL || remote_description() == NULL) {
694 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
695 << "SSL Role of the session.";
696 return false;
697 }
698
699 // TODO(mallinath) - Return role of each transport, as role may differ from
700 // one another.
701 // In current implementaion we just return the role of first transport in the
702 // transport map.
703 for (cricket::TransportMap::const_iterator iter = transport_proxies().begin();
704 iter != transport_proxies().end(); ++iter) {
705 if (iter->second->impl()) {
706 return iter->second->impl()->GetSslRole(role);
707 }
708 }
709 return false;
710}
711
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000712void WebRtcSession::CreateOffer(
713 CreateSessionDescriptionObserver* observer,
714 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
715 webrtc_session_desc_factory_->CreateOffer(observer, options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000716}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717
wu@webrtc.org91053e72013-08-10 07:18:04 +0000718void WebRtcSession::CreateAnswer(CreateSessionDescriptionObserver* observer,
719 const MediaConstraintsInterface* constraints) {
720 webrtc_session_desc_factory_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721}
722
723bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
724 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000725 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000726 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000727
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000728 // Validate SDP.
729 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
730 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 }
732
733 // Update the initiator flag if this session is the initiator.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000734 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 if (state() == STATE_INIT && action == kOffer) {
736 set_initiator(true);
737 }
738
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000739 cricket::SecurePolicy sdes_policy =
740 webrtc_session_desc_factory_->SdesPolicy();
741 cricket::CryptoType crypto_required = dtls_enabled_ ?
742 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
743 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000745 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746
747 set_local_description(desc->description()->Copy());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000748 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000749
750 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000751 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000752 // TODO(mallinath) - Handle CreateChannel failure, as new local description
753 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000754 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755 }
756
757 // Remove channel and transport proxies, if MediaContentDescription is
758 // rejected.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000759 RemoveUnusedChannelsAndTransports(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000761 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762 return false;
763 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000764
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 // Kick starting the ice candidates allocation.
766 StartCandidatesAllocation();
767
768 // Update state and SSRC of local MediaStreams and DataChannels based on the
769 // local session description.
770 mediastream_signaling_->OnLocalDescriptionChanged(local_desc_.get());
771
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000772 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000773 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
774 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
775 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000777 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778 }
779 return true;
780}
781
782bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
783 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000784 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000785 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000786
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000787 // Validate SDP.
788 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
789 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 }
791
792 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000793 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794 if (action == kOffer && !CreateChannels(desc->description())) {
795 // TODO(mallinath) - Handle CreateChannel failure, as new local description
796 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000797 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 }
799
800 // Remove channel and transport proxies, if MediaContentDescription is
801 // rejected.
802 RemoveUnusedChannelsAndTransports(desc->description());
803
804 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
805 // is called.
806 set_remote_description(desc->description()->Copy());
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000807 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808 return false;
809 }
810
811 // Update remote MediaStreams.
812 mediastream_signaling_->OnRemoteDescriptionChanged(desc);
813 if (local_description() && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000814 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 }
816
817 // Copy all saved candidates.
818 CopySavedCandidates(desc);
819 // We retain all received candidates.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000820 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
821 remote_desc_.get(), desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 // Check if this new SessionDescription contains new ice ufrag and password
823 // that indicates the remote peer requests ice restart.
824 ice_restart_latch_->CheckForRemoteIceRestart(remote_desc_.get(),
825 desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000826 remote_desc_.reset(desc_temp.release());
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000827
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000828 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000829 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
830 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
831 }
832
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000833 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000834 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835 }
836 return true;
837}
838
839bool WebRtcSession::UpdateSessionState(
840 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841 std::string* err_desc) {
842 // If there's already a pending error then no state transition should happen.
843 // But all call-sites should be verifying this before calling us!
844 ASSERT(error() == cricket::BaseSession::ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000845 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000846 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000847 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
848 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 }
850 SetState(source == cricket::CS_LOCAL ?
851 STATE_SENTINITIATE : STATE_RECEIVEDINITIATE);
852 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000853 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854 }
855 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000856 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
857 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 }
859 EnableChannels();
860 SetState(source == cricket::CS_LOCAL ?
861 STATE_SENTPRACCEPT : STATE_RECEIVEDPRACCEPT);
862 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000863 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000864 }
865 } else if (action == kAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000866 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
867 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868 }
869 MaybeEnableMuxingSupport();
870 EnableChannels();
871 SetState(source == cricket::CS_LOCAL ?
872 STATE_SENTACCEPT : STATE_RECEIVEDACCEPT);
873 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000874 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 }
876 }
877 return true;
878}
879
880WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
881 if (type == SessionDescriptionInterface::kOffer) {
882 return WebRtcSession::kOffer;
883 } else if (type == SessionDescriptionInterface::kPrAnswer) {
884 return WebRtcSession::kPrAnswer;
885 } else if (type == SessionDescriptionInterface::kAnswer) {
886 return WebRtcSession::kAnswer;
887 }
888 ASSERT(false && "unknown action type");
889 return WebRtcSession::kOffer;
890}
891
892bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
893 if (state() == STATE_INIT) {
894 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
895 << "without any offer (local or remote) "
896 << "session description.";
897 return false;
898 }
899
900 if (!candidate) {
901 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL";
902 return false;
903 }
904
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +0000905 bool valid = false;
906 if (!ReadyToUseRemoteCandidate(candidate, NULL, &valid)) {
907 if (valid) {
908 LOG(LS_INFO) << "ProcessIceMessage: Candidate saved";
909 saved_candidates_.push_back(
910 new JsepIceCandidate(candidate->sdp_mid(),
911 candidate->sdp_mline_index(),
912 candidate->candidate()));
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000913 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +0000914 return valid;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 }
916
917 // Add this candidate to the remote session description.
918 if (!remote_desc_->AddCandidate(candidate)) {
919 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used";
920 return false;
921 }
922
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000923 return UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000924}
925
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000926bool WebRtcSession::SetIceTransports(
927 PeerConnectionInterface::IceTransportsType type) {
928 return port_allocator()->set_candidate_filter(
929 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000930}
931
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000932bool WebRtcSession::GetLocalTrackIdBySsrc(uint32 ssrc, std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000933 if (!BaseSession::local_description())
934 return false;
935 return webrtc::GetTrackIdBySsrc(
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000936 BaseSession::local_description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000937}
938
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000939bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32 ssrc, std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000940 if (!BaseSession::remote_description())
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000941 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942 return webrtc::GetTrackIdBySsrc(
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000943 BaseSession::remote_description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944}
945
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000946std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000947 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000948 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949 return desc.str();
950}
951
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000952void WebRtcSession::SetAudioPlayout(uint32 ssrc, bool enable,
953 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954 ASSERT(signaling_thread()->IsCurrent());
955 if (!voice_channel_) {
956 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
957 return;
958 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000959 if (!voice_channel_->SetRemoteRenderer(ssrc, renderer)) {
960 // SetRenderer() can fail if the ssrc does not match any playout channel.
961 LOG(LS_ERROR) << "SetAudioPlayout: ssrc is incorrect: " << ssrc;
962 return;
963 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964 if (!voice_channel_->SetOutputScaling(ssrc, enable ? 1 : 0, enable ? 1 : 0)) {
965 // Allow that SetOutputScaling fail if |enable| is false but assert
966 // otherwise. This in the normal case when the underlying media channel has
967 // already been deleted.
968 ASSERT(enable == false);
969 }
970}
971
972void WebRtcSession::SetAudioSend(uint32 ssrc, bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000973 const cricket::AudioOptions& options,
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) << "SetAudioSend: No audio channel exists.";
978 return;
979 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000980 if (!voice_channel_->SetLocalRenderer(ssrc, renderer)) {
981 // SetRenderer() can fail if the ssrc does not match any send channel.
982 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
983 return;
984 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000985 if (!voice_channel_->MuteStream(ssrc, !enable)) {
986 // Allow that MuteStream fail if |enable| is false but assert otherwise.
987 // This in the normal case when the underlying media channel has already
988 // been deleted.
989 ASSERT(enable == false);
990 return;
991 }
992 if (enable)
993 voice_channel_->SetChannelOptions(options);
994}
995
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000996void WebRtcSession::SetAudioPlayoutVolume(uint32 ssrc, double volume) {
997 ASSERT(signaling_thread()->IsCurrent());
998 ASSERT(volume >= 0 && volume <= 10);
999 if (!voice_channel_) {
1000 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1001 return;
1002 }
1003
1004 if (!voice_channel_->SetOutputScaling(ssrc, volume, volume))
1005 ASSERT(false);
1006}
1007
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001008bool WebRtcSession::SetCaptureDevice(uint32 ssrc,
1009 cricket::VideoCapturer* camera) {
1010 ASSERT(signaling_thread()->IsCurrent());
1011
1012 if (!video_channel_.get()) {
1013 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1014 // support video.
1015 LOG(LS_WARNING) << "Video not used in this call.";
1016 return false;
1017 }
1018 if (!video_channel_->SetCapturer(ssrc, camera)) {
1019 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1020 // This in the normal case when the underlying media channel has already
1021 // been deleted.
1022 ASSERT(camera == NULL);
1023 return false;
1024 }
1025 return true;
1026}
1027
1028void WebRtcSession::SetVideoPlayout(uint32 ssrc,
1029 bool enable,
1030 cricket::VideoRenderer* renderer) {
1031 ASSERT(signaling_thread()->IsCurrent());
1032 if (!video_channel_) {
1033 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1034 return;
1035 }
1036 if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) {
1037 // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise.
1038 // This in the normal case when the underlying media channel has already
1039 // been deleted.
1040 ASSERT(renderer == NULL);
1041 }
1042}
1043
1044void WebRtcSession::SetVideoSend(uint32 ssrc, bool enable,
1045 const cricket::VideoOptions* options) {
1046 ASSERT(signaling_thread()->IsCurrent());
1047 if (!video_channel_) {
1048 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1049 return;
1050 }
1051 if (!video_channel_->MuteStream(ssrc, !enable)) {
1052 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1053 // This in the normal case when the underlying media channel has already
1054 // been deleted.
1055 ASSERT(enable == false);
1056 return;
1057 }
1058 if (enable && options)
1059 video_channel_->SetChannelOptions(*options);
1060}
1061
1062bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1063 ASSERT(signaling_thread()->IsCurrent());
1064 if (!voice_channel_) {
1065 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1066 return false;
1067 }
1068 uint32 send_ssrc = 0;
1069 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1070 // exists.
1071 if (!GetAudioSsrcByTrackId(BaseSession::local_description(), track_id,
1072 &send_ssrc)) {
1073 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1074 return false;
1075 }
1076 return voice_channel_->CanInsertDtmf();
1077}
1078
1079bool WebRtcSession::InsertDtmf(const std::string& track_id,
1080 int code, int duration) {
1081 ASSERT(signaling_thread()->IsCurrent());
1082 if (!voice_channel_) {
1083 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1084 return false;
1085 }
1086 uint32 send_ssrc = 0;
1087 if (!VERIFY(GetAudioSsrcByTrackId(BaseSession::local_description(),
1088 track_id, &send_ssrc))) {
1089 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1090 return false;
1091 }
1092 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration,
1093 cricket::DF_SEND)) {
1094 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1095 return false;
1096 }
1097 return true;
1098}
1099
1100sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
1101 return &SignalVoiceChannelDestroyed;
1102}
1103
wu@webrtc.org78187522013-10-07 23:32:02 +00001104bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001105 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001106 cricket::SendDataResult* result) {
1107 if (!data_channel_.get()) {
1108 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1109 return false;
1110 }
1111 return data_channel_->SendData(params, payload, result);
1112}
1113
1114bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
1115 if (!data_channel_.get()) {
1116 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1117 return false;
1118 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001119 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1120 &DataChannel::OnChannelReady);
1121 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1122 &DataChannel::OnDataReceived);
wu@webrtc.org78187522013-10-07 23:32:02 +00001123 return true;
1124}
1125
1126void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001127 if (!data_channel_.get()) {
1128 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1129 return;
1130 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001131 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1132 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
1133}
1134
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001135void WebRtcSession::AddSctpDataStream(int sid) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001136 if (!data_channel_.get()) {
1137 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1138 return;
1139 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001140 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1141 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001142}
1143
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001144void WebRtcSession::RemoveSctpDataStream(int sid) {
1145 mediastream_signaling_->RemoveSctpDataChannel(sid);
jiayl@webrtc.org2eaac182014-06-17 16:02:46 +00001146
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001147 if (!data_channel_.get()) {
1148 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1149 << "NULL.";
1150 return;
1151 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001152 data_channel_->RemoveRecvStream(sid);
1153 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001154}
1155
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001156bool WebRtcSession::ReadyToSendData() const {
1157 return data_channel_.get() && data_channel_->ready_to_send_data();
1158}
1159
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001160rtc::scoped_refptr<DataChannel> WebRtcSession::CreateDataChannel(
wu@webrtc.org78187522013-10-07 23:32:02 +00001161 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001162 const InternalDataChannelInit* config) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001163 if (state() == STATE_RECEIVEDTERMINATE) {
1164 return NULL;
1165 }
1166 if (data_channel_type_ == cricket::DCT_NONE) {
1167 LOG(LS_ERROR) << "CreateDataChannel: Data is not supported in this call.";
1168 return NULL;
1169 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001170 InternalDataChannelInit new_config =
1171 config ? (*config) : InternalDataChannelInit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001172 if (data_channel_type_ == cricket::DCT_SCTP) {
1173 if (new_config.id < 0) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001174 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001175 if (GetSslRole(&role) &&
1176 !mediastream_signaling_->AllocateSctpSid(role, &new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001177 LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel.";
1178 return NULL;
1179 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001180 } else if (!mediastream_signaling_->IsSctpSidAvailable(new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181 LOG(LS_ERROR) << "Failed to create a SCTP data channel "
1182 << "because the id is already in use or out of range.";
1183 return NULL;
1184 }
1185 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001186
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001187 rtc::scoped_refptr<DataChannel> channel(DataChannel::Create(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001188 this, data_channel_type_, label, new_config));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001189 if (channel && !mediastream_signaling_->AddDataChannel(channel))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001190 return NULL;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001191
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001192 return channel;
1193}
1194
1195cricket::DataChannelType WebRtcSession::data_channel_type() const {
1196 return data_channel_type_;
1197}
1198
wu@webrtc.org91053e72013-08-10 07:18:04 +00001199bool WebRtcSession::IceRestartPending() const {
1200 return ice_restart_latch_->Get();
1201}
1202
1203void WebRtcSession::ResetIceRestartLatch() {
1204 ice_restart_latch_->Reset();
1205}
1206
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001207void WebRtcSession::OnIdentityReady(rtc::SSLIdentity* identity) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001208 SetIdentity(identity);
1209}
1210
1211bool WebRtcSession::waiting_for_identity() const {
1212 return webrtc_session_desc_factory_->waiting_for_identity();
1213}
1214
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001215void WebRtcSession::SetIceConnectionState(
1216 PeerConnectionInterface::IceConnectionState state) {
1217 if (ice_connection_state_ == state) {
1218 return;
1219 }
1220
1221 // ASSERT that the requested transition is allowed. Note that
1222 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1223 // within PeerConnection). This switch statement should compile away when
1224 // ASSERTs are disabled.
1225 switch (ice_connection_state_) {
1226 case PeerConnectionInterface::kIceConnectionNew:
1227 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1228 break;
1229 case PeerConnectionInterface::kIceConnectionChecking:
1230 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1231 state == PeerConnectionInterface::kIceConnectionConnected);
1232 break;
1233 case PeerConnectionInterface::kIceConnectionConnected:
1234 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1235 state == PeerConnectionInterface::kIceConnectionChecking ||
1236 state == PeerConnectionInterface::kIceConnectionCompleted);
1237 break;
1238 case PeerConnectionInterface::kIceConnectionCompleted:
1239 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1240 state == PeerConnectionInterface::kIceConnectionDisconnected);
1241 break;
1242 case PeerConnectionInterface::kIceConnectionFailed:
1243 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1244 break;
1245 case PeerConnectionInterface::kIceConnectionDisconnected:
1246 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1247 state == PeerConnectionInterface::kIceConnectionConnected ||
1248 state == PeerConnectionInterface::kIceConnectionCompleted ||
1249 state == PeerConnectionInterface::kIceConnectionFailed);
1250 break;
1251 case PeerConnectionInterface::kIceConnectionClosed:
1252 ASSERT(false);
1253 break;
1254 default:
1255 ASSERT(false);
1256 break;
1257 }
1258
1259 ice_connection_state_ = state;
1260 if (ice_observer_) {
1261 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1262 }
1263}
1264
1265void WebRtcSession::OnTransportRequestSignaling(
1266 cricket::Transport* transport) {
1267 ASSERT(signaling_thread()->IsCurrent());
1268 transport->OnSignalingReady();
1269 if (ice_observer_) {
1270 ice_observer_->OnIceGatheringChange(
1271 PeerConnectionInterface::kIceGatheringGathering);
1272 }
1273}
1274
1275void WebRtcSession::OnTransportConnecting(cricket::Transport* transport) {
1276 ASSERT(signaling_thread()->IsCurrent());
1277 // start monitoring for the write state of the transport.
1278 OnTransportWritable(transport);
1279}
1280
1281void WebRtcSession::OnTransportWritable(cricket::Transport* transport) {
1282 ASSERT(signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001283 if (transport->all_channels_writable()) {
henrike@webrtc.org05376342014-03-10 15:53:12 +00001284 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001285 } else if (transport->HasChannels()) {
1286 // If the current state is Connected or Completed, then there were writable
1287 // channels but now there are not, so the next state must be Disconnected.
1288 if (ice_connection_state_ ==
1289 PeerConnectionInterface::kIceConnectionConnected ||
1290 ice_connection_state_ ==
1291 PeerConnectionInterface::kIceConnectionCompleted) {
1292 SetIceConnectionState(
1293 PeerConnectionInterface::kIceConnectionDisconnected);
1294 }
1295 }
1296}
1297
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001298void WebRtcSession::OnTransportCompleted(cricket::Transport* transport) {
1299 ASSERT(signaling_thread()->IsCurrent());
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001300 PeerConnectionInterface::IceConnectionState old_state = ice_connection_state_;
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001301 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001302 // Only report once when Ice connection is completed.
1303 if (old_state != PeerConnectionInterface::kIceConnectionCompleted) {
1304 ReportBestConnectionState(transport);
1305 }
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001306}
1307
1308void WebRtcSession::OnTransportFailed(cricket::Transport* transport) {
1309 ASSERT(signaling_thread()->IsCurrent());
1310 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1311}
1312
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001313void WebRtcSession::OnTransportProxyCandidatesReady(
1314 cricket::TransportProxy* proxy, const cricket::Candidates& candidates) {
1315 ASSERT(signaling_thread()->IsCurrent());
1316 ProcessNewLocalCandidate(proxy->content_name(), candidates);
1317}
1318
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001319void WebRtcSession::OnCandidatesAllocationDone() {
1320 ASSERT(signaling_thread()->IsCurrent());
1321 if (ice_observer_) {
1322 ice_observer_->OnIceGatheringChange(
1323 PeerConnectionInterface::kIceGatheringComplete);
1324 ice_observer_->OnIceComplete();
1325 }
1326}
1327
1328// Enabling voice and video channel.
1329void WebRtcSession::EnableChannels() {
1330 if (voice_channel_ && !voice_channel_->enabled())
1331 voice_channel_->Enable(true);
1332
1333 if (video_channel_ && !video_channel_->enabled())
1334 video_channel_->Enable(true);
1335
1336 if (data_channel_.get() && !data_channel_->enabled())
1337 data_channel_->Enable(true);
1338}
1339
1340void WebRtcSession::ProcessNewLocalCandidate(
1341 const std::string& content_name,
1342 const cricket::Candidates& candidates) {
1343 int sdp_mline_index;
1344 if (!GetLocalCandidateMediaIndex(content_name, &sdp_mline_index)) {
1345 LOG(LS_ERROR) << "ProcessNewLocalCandidate: content name "
1346 << content_name << " not found";
1347 return;
1348 }
1349
1350 for (cricket::Candidates::const_iterator citer = candidates.begin();
1351 citer != candidates.end(); ++citer) {
1352 // Use content_name as the candidate media id.
1353 JsepIceCandidate candidate(content_name, sdp_mline_index, *citer);
1354 if (ice_observer_) {
1355 ice_observer_->OnIceCandidate(&candidate);
1356 }
1357 if (local_desc_) {
1358 local_desc_->AddCandidate(&candidate);
1359 }
1360 }
1361}
1362
1363// Returns the media index for a local ice candidate given the content name.
1364bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1365 int* sdp_mline_index) {
1366 if (!BaseSession::local_description() || !sdp_mline_index)
1367 return false;
1368
1369 bool content_found = false;
1370 const ContentInfos& contents = BaseSession::local_description()->contents();
1371 for (size_t index = 0; index < contents.size(); ++index) {
1372 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001373 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001374 content_found = true;
1375 break;
1376 }
1377 }
1378 return content_found;
1379}
1380
1381bool WebRtcSession::UseCandidatesInSessionDescription(
1382 const SessionDescriptionInterface* remote_desc) {
1383 if (!remote_desc)
1384 return true;
1385 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001386
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001387 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1388 const IceCandidateCollection* candidates = remote_desc->candidates(m);
1389 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001390 const IceCandidateInterface* candidate = candidates->at(n);
1391 bool valid = false;
1392 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1393 if (valid) {
1394 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Candidate saved.";
1395 saved_candidates_.push_back(
1396 new JsepIceCandidate(candidate->sdp_mid(),
1397 candidate->sdp_mline_index(),
1398 candidate->candidate()));
1399 }
1400 continue;
1401 }
1402
1403 ret = UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001404 if (!ret)
1405 break;
1406 }
1407 }
1408 return ret;
1409}
1410
1411bool WebRtcSession::UseCandidate(
1412 const IceCandidateInterface* candidate) {
1413
1414 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
1415 size_t remote_content_size =
1416 BaseSession::remote_description()->contents().size();
1417 if (mediacontent_index >= remote_content_size) {
1418 LOG(LS_ERROR)
1419 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1420 return false;
1421 }
1422
1423 cricket::ContentInfo content =
1424 BaseSession::remote_description()->contents()[mediacontent_index];
1425 std::vector<cricket::Candidate> candidates;
1426 candidates.push_back(candidate->candidate());
1427 // Invoking BaseSession method to handle remote candidates.
1428 std::string error;
1429 if (OnRemoteCandidates(content.name, candidates, &error)) {
1430 // Candidates successfully submitted for checking.
1431 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1432 ice_connection_state_ ==
1433 PeerConnectionInterface::kIceConnectionDisconnected) {
1434 // If state is New, then the session has just gotten its first remote ICE
1435 // candidates, so go to Checking.
1436 // If state is Disconnected, the session is re-using old candidates or
1437 // receiving additional ones, so go to Checking.
1438 // If state is Connected, stay Connected.
1439 // TODO(bemasc): If state is Connected, and the new candidates are for a
1440 // newly added transport, then the state actually _should_ move to
1441 // checking. Add a way to distinguish that case.
1442 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1443 }
1444 // TODO(bemasc): If state is Completed, go back to Connected.
1445 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001446 if (!error.empty()) {
1447 LOG(LS_WARNING) << error;
1448 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001449 }
1450 return true;
1451}
1452
1453void WebRtcSession::RemoveUnusedChannelsAndTransports(
1454 const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001455 // Destroy video_channel_ first since it may have a pointer to the
1456 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001457 const cricket::ContentInfo* video_info =
1458 cricket::GetFirstVideoContent(desc);
1459 if ((!video_info || video_info->rejected) && video_channel_) {
1460 mediastream_signaling_->OnVideoChannelClose();
1461 SignalVideoChannelDestroyed();
1462 const std::string content_name = video_channel_->content_name();
1463 channel_manager_->DestroyVideoChannel(video_channel_.release());
1464 DestroyTransportProxy(content_name);
1465 }
1466
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001467 const cricket::ContentInfo* voice_info =
1468 cricket::GetFirstAudioContent(desc);
1469 if ((!voice_info || voice_info->rejected) && voice_channel_) {
1470 mediastream_signaling_->OnAudioChannelClose();
1471 SignalVoiceChannelDestroyed();
1472 const std::string content_name = voice_channel_->content_name();
1473 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
1474 DestroyTransportProxy(content_name);
1475 }
1476
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001477 const cricket::ContentInfo* data_info =
1478 cricket::GetFirstDataContent(desc);
1479 if ((!data_info || data_info->rejected) && data_channel_) {
1480 mediastream_signaling_->OnDataChannelClose();
1481 SignalDataChannelDestroyed();
1482 const std::string content_name = data_channel_->content_name();
1483 channel_manager_->DestroyDataChannel(data_channel_.release());
1484 DestroyTransportProxy(content_name);
1485 }
1486}
1487
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001488// TODO(mallinath) - Add a correct error code if the channels are not creatued
1489// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001490bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
1491 // Disabling the BUNDLE flag in PortAllocator if offer disabled it.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001492 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1493 if (state() == STATE_INIT && !bundle_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001494 port_allocator()->set_flags(port_allocator()->flags() &
1495 ~cricket::PORTALLOCATOR_ENABLE_BUNDLE);
1496 }
1497
1498 // Creating the media channels and transport proxies.
1499 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1500 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001501 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001502 LOG(LS_ERROR) << "Failed to create voice channel.";
1503 return false;
1504 }
1505 }
1506
1507 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1508 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001509 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001510 LOG(LS_ERROR) << "Failed to create video channel.";
1511 return false;
1512 }
1513 }
1514
1515 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1516 if (data_channel_type_ != cricket::DCT_NONE &&
1517 data && !data->rejected && !data_channel_.get()) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001518 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001519 LOG(LS_ERROR) << "Failed to create data channel.";
1520 return false;
1521 }
1522 }
1523
1524 return true;
1525}
1526
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001527bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001528 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001529 this, content->name, true));
wu@webrtc.orgde305012013-10-31 15:40:38 +00001530 if (!voice_channel_.get())
1531 return false;
1532
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00001533 voice_channel_->SetChannelOptions(audio_options_);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001534 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001535}
1536
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001537bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001538 video_channel_.reset(channel_manager_->CreateVideoChannel(
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +00001539 this, content->name, true, video_options_, voice_channel_.get()));
1540 return video_channel_.get() != NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001541}
1542
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001543bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001544 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001545 data_channel_.reset(channel_manager_->CreateDataChannel(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001546 this, content->name, !sctp, data_channel_type_));
wu@webrtc.org91053e72013-08-10 07:18:04 +00001547 if (!data_channel_.get()) {
1548 return false;
1549 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001550 if (sctp) {
1551 mediastream_signaling_->OnDataTransportCreatedForSctp();
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001552 data_channel_->SignalDataReceived.connect(
1553 this, &WebRtcSession::OnDataChannelMessageReceived);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00001554 data_channel_->SignalStreamClosedRemotely.connect(
1555 mediastream_signaling_,
1556 &MediaStreamSignaling::OnRemoteSctpDataChannelClosed);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001557 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001558 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001559}
1560
1561void WebRtcSession::CopySavedCandidates(
1562 SessionDescriptionInterface* dest_desc) {
1563 if (!dest_desc) {
1564 ASSERT(false);
1565 return;
1566 }
1567 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
1568 dest_desc->AddCandidate(saved_candidates_[i]);
1569 delete saved_candidates_[i];
1570 }
1571 saved_candidates_.clear();
1572}
1573
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001574void WebRtcSession::OnDataChannelMessageReceived(
1575 cricket::DataChannel* channel,
1576 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001577 const rtc::Buffer& payload) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001578 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001579 if (params.type == cricket::DMT_CONTROL &&
1580 mediastream_signaling_->IsSctpSidAvailable(params.ssrc)) {
1581 // Received CONTROL on unused sid, process as an OPEN message.
1582 mediastream_signaling_->AddDataChannelFromOpenMessage(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001583 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001584 // otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001585}
1586
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001587// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001588bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001589 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1590 if (!bundle_enabled)
1591 return true;
1592
1593 const cricket::ContentGroup* bundle_group =
1594 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1595 ASSERT(bundle_group != NULL);
1596
1597 const cricket::ContentInfos& contents = desc->contents();
1598 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1599 citer != contents.end(); ++citer) {
1600 const cricket::ContentInfo* content = (&*citer);
1601 ASSERT(content != NULL);
1602 if (bundle_group->HasContentName(content->name) &&
1603 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1604 if (!HasRtcpMuxEnabled(content))
1605 return false;
1606 }
1607 }
1608 // RTCP-MUX is enabled in all the contents.
1609 return true;
1610}
1611
1612bool WebRtcSession::HasRtcpMuxEnabled(
1613 const cricket::ContentInfo* content) {
1614 const cricket::MediaContentDescription* description =
1615 static_cast<cricket::MediaContentDescription*>(content->description);
1616 return description->rtcp_mux();
1617}
1618
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001619bool WebRtcSession::ValidateSessionDescription(
1620 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001621 cricket::ContentSource source, std::string* err_desc) {
1622 std::string type;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001623 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001624 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001625 }
1626
1627 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001628 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001629 }
1630
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001631 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001632 Action action = GetAction(sdesc->type());
1633 if (source == cricket::CS_LOCAL) {
1634 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001635 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001636 } else {
1637 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001638 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001639 }
1640
1641 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001642 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001643 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1644 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001645 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001646 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001647 }
1648
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001649 // Verify ice-ufrag and ice-pwd.
1650 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001651 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001652 }
1653
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001654 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001655 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001656 }
1657
1658 // Verify m-lines in Answer when compared against Offer.
1659 if (action == kAnswer) {
1660 const cricket::SessionDescription* offer_desc =
1661 (source == cricket::CS_LOCAL) ? remote_description()->description() :
1662 local_description()->description();
1663 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001664 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001665 }
1666 }
1667
1668 return true;
1669}
1670
1671bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1672 return ((action == kOffer && state() == STATE_INIT) ||
1673 // update local offer
1674 (action == kOffer && state() == STATE_SENTINITIATE) ||
1675 // update the current ongoing session.
1676 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1677 (action == kOffer && state() == STATE_SENTACCEPT) ||
1678 (action == kOffer && state() == STATE_INPROGRESS) ||
1679 // accept remote offer
1680 (action == kAnswer && state() == STATE_RECEIVEDINITIATE) ||
1681 (action == kAnswer && state() == STATE_SENTPRACCEPT) ||
1682 (action == kPrAnswer && state() == STATE_RECEIVEDINITIATE) ||
1683 (action == kPrAnswer && state() == STATE_SENTPRACCEPT));
1684}
1685
1686bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1687 return ((action == kOffer && state() == STATE_INIT) ||
1688 // update remote offer
1689 (action == kOffer && state() == STATE_RECEIVEDINITIATE) ||
1690 // update the current ongoing session
1691 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1692 (action == kOffer && state() == STATE_SENTACCEPT) ||
1693 (action == kOffer && state() == STATE_INPROGRESS) ||
1694 // accept local offer
1695 (action == kAnswer && state() == STATE_SENTINITIATE) ||
1696 (action == kAnswer && state() == STATE_RECEIVEDPRACCEPT) ||
1697 (action == kPrAnswer && state() == STATE_SENTINITIATE) ||
1698 (action == kPrAnswer && state() == STATE_RECEIVEDPRACCEPT));
1699}
1700
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001701std::string WebRtcSession::GetSessionErrorMsg() {
1702 std::ostringstream desc;
1703 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1704 desc << kSessionErrorDesc << error_desc() << ".";
1705 return desc.str();
1706}
1707
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001708// We need to check the local/remote description for the Transport instead of
1709// the session, because a new Transport added during renegotiation may have
1710// them unset while the session has them set from the previous negotiation.
1711// Not doing so may trigger the auto generation of transport description and
1712// mess up DTLS identity information, ICE credential, etc.
1713bool WebRtcSession::ReadyToUseRemoteCandidate(
1714 const IceCandidateInterface* candidate,
1715 const SessionDescriptionInterface* remote_desc,
1716 bool* valid) {
1717 *valid = true;;
1718 cricket::TransportProxy* transport_proxy = NULL;
1719
1720 const SessionDescriptionInterface* current_remote_desc =
1721 remote_desc ? remote_desc : remote_description();
1722
1723 if (!current_remote_desc)
1724 return false;
1725
1726 size_t mediacontent_index =
1727 static_cast<size_t>(candidate->sdp_mline_index());
1728 size_t remote_content_size =
1729 current_remote_desc->description()->contents().size();
1730 if (mediacontent_index >= remote_content_size) {
1731 LOG(LS_ERROR)
1732 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
1733
1734 *valid = false;
1735 return false;
1736 }
1737
1738 cricket::ContentInfo content =
1739 current_remote_desc->description()->contents()[mediacontent_index];
1740 transport_proxy = GetTransportProxy(content.name);
1741
1742 return transport_proxy && transport_proxy->local_description_set() &&
1743 transport_proxy->remote_description_set();
1744}
1745
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001746// Walk through the ConnectionInfos to gather best connection usage
1747// for IPv4 and IPv6.
1748void WebRtcSession::ReportBestConnectionState(cricket::Transport* transport) {
1749 if (!metrics_observer_) {
1750 return;
1751 }
1752
1753 cricket::TransportStats stats;
1754 if (!transport->GetStats(&stats)) {
1755 return;
1756 }
1757
1758 for (cricket::TransportChannelStatsList::const_iterator it =
1759 stats.channel_stats.begin();
1760 it != stats.channel_stats.end(); ++it) {
1761 for (cricket::ConnectionInfos::const_iterator it_info =
1762 it->connection_infos.begin();
1763 it_info != it->connection_infos.end(); ++it_info) {
1764 if (!it_info->best_connection) {
1765 continue;
1766 }
1767 if (it_info->local_candidate.address().family() == AF_INET) {
1768 metrics_observer_->IncrementCounter(kBestConnections_IPv4);
1769 } else if (it_info->local_candidate.address().family() ==
1770 AF_INET6) {
1771 metrics_observer_->IncrementCounter(kBestConnections_IPv6);
1772 } else {
1773 ASSERT(false);
1774 }
1775 return;
1776 }
1777 }
1778}
1779
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001780} // namespace webrtc