blob: 683efac13fd3b8cff238ce9b3b0f7a607aead32a [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2012, Google Inc.
4 *
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 // Find payload padding constraint.
597 SetOptionFromOptionalConstraint(constraints,
598 MediaConstraintsInterface::kPayloadPadding,
599 &video_options_.use_payload_padding);
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +0000600
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000601 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000602 MediaConstraintsInterface::kNumUnsignalledRecvStreams,
603 &video_options_.unsignalled_recv_stream_limit);
604 if (video_options_.unsignalled_recv_stream_limit.IsSet()) {
605 int stream_limit;
606 video_options_.unsignalled_recv_stream_limit.Get(&stream_limit);
607 stream_limit = rtc::_min(kMaxUnsignalledRecvStreams, stream_limit);
608 stream_limit = rtc::_max(0, stream_limit);
609 video_options_.unsignalled_recv_stream_limit.Set(stream_limit);
610 }
611
612 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000613 MediaConstraintsInterface::kHighStartBitrate,
614 &video_options_.video_start_bitrate);
wu@webrtc.orgcfe5e9c2014-03-27 17:03:58 +0000615
616 if (FindConstraint(
617 constraints,
618 MediaConstraintsInterface::kVeryHighBitrate,
619 &value,
620 NULL)) {
621 video_options_.video_highest_bitrate.Set(
622 cricket::VideoOptions::VERY_HIGH);
623 } else if (FindConstraint(
624 constraints,
625 MediaConstraintsInterface::kHighBitrate,
626 &value,
627 NULL)) {
628 video_options_.video_highest_bitrate.Set(
629 cricket::VideoOptions::HIGH);
630 }
631
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000632 SetOptionFromOptionalConstraint(constraints,
633 MediaConstraintsInterface::kCombinedAudioVideoBwe,
634 &audio_options_.combined_audio_video_bwe);
635
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636 const cricket::VideoCodec default_codec(
637 JsepSessionDescription::kDefaultVideoCodecId,
638 JsepSessionDescription::kDefaultVideoCodecName,
639 JsepSessionDescription::kMaxVideoCodecWidth,
640 JsepSessionDescription::kMaxVideoCodecHeight,
641 JsepSessionDescription::kDefaultVideoCodecFramerate,
642 JsepSessionDescription::kDefaultVideoCodecPreference);
643 channel_manager_->SetDefaultVideoEncoderConfig(
644 cricket::VideoEncoderConfig(default_codec));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000645
646 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
647 signaling_thread(),
648 channel_manager_,
649 mediastream_signaling_,
650 dtls_identity_service,
651 this,
652 id(),
653 data_channel_type_,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000654 dtls_enabled_));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000655
656 webrtc_session_desc_factory_->SignalIdentityReady.connect(
657 this, &WebRtcSession::OnIdentityReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000658
wu@webrtc.org97077a32013-10-25 21:18:33 +0000659 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000660 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000661 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000662 port_allocator()->set_candidate_filter(
663 ConvertIceTransportTypeToCandidateFilter(ice_transport));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000664 return true;
665}
666
667void WebRtcSession::Terminate() {
668 SetState(STATE_RECEIVEDTERMINATE);
669 RemoveUnusedChannelsAndTransports(NULL);
670 ASSERT(voice_channel_.get() == NULL);
671 ASSERT(video_channel_.get() == NULL);
672 ASSERT(data_channel_.get() == NULL);
673}
674
675bool WebRtcSession::StartCandidatesAllocation() {
676 // SpeculativelyConnectTransportChannels, will call ConnectChannels method
677 // from TransportProxy to start gathering ice candidates.
678 SpeculativelyConnectAllTransportChannels();
679 if (!saved_candidates_.empty()) {
680 // If there are saved candidates which arrived before local description is
681 // set, copy those to remote description.
682 CopySavedCandidates(remote_desc_.get());
683 }
684 // Push remote candidates present in remote description to transport channels.
685 UseCandidatesInSessionDescription(remote_desc_.get());
686 return true;
687}
688
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000689void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
690 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000691}
692
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000693cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
694 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695}
696
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000697bool WebRtcSession::GetSslRole(rtc::SSLRole* role) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000698 if (local_description() == NULL || remote_description() == NULL) {
699 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
700 << "SSL Role of the session.";
701 return false;
702 }
703
704 // TODO(mallinath) - Return role of each transport, as role may differ from
705 // one another.
706 // In current implementaion we just return the role of first transport in the
707 // transport map.
708 for (cricket::TransportMap::const_iterator iter = transport_proxies().begin();
709 iter != transport_proxies().end(); ++iter) {
710 if (iter->second->impl()) {
711 return iter->second->impl()->GetSslRole(role);
712 }
713 }
714 return false;
715}
716
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000717void WebRtcSession::CreateOffer(
718 CreateSessionDescriptionObserver* observer,
719 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
720 webrtc_session_desc_factory_->CreateOffer(observer, options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000721}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722
wu@webrtc.org91053e72013-08-10 07:18:04 +0000723void WebRtcSession::CreateAnswer(CreateSessionDescriptionObserver* observer,
724 const MediaConstraintsInterface* constraints) {
725 webrtc_session_desc_factory_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726}
727
728bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
729 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000730 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000731 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000732
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000733 // Validate SDP.
734 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
735 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000736 }
737
738 // Update the initiator flag if this session is the initiator.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000739 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000740 if (state() == STATE_INIT && action == kOffer) {
741 set_initiator(true);
742 }
743
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000744 cricket::SecurePolicy sdes_policy =
745 webrtc_session_desc_factory_->SdesPolicy();
746 cricket::CryptoType crypto_required = dtls_enabled_ ?
747 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
748 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000749 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000750 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000751
752 set_local_description(desc->description()->Copy());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000753 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000754
755 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000756 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 // TODO(mallinath) - Handle CreateChannel failure, as new local description
758 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000759 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760 }
761
762 // Remove channel and transport proxies, if MediaContentDescription is
763 // rejected.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000764 RemoveUnusedChannelsAndTransports(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000766 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767 return false;
768 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000769
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770 // Kick starting the ice candidates allocation.
771 StartCandidatesAllocation();
772
773 // Update state and SSRC of local MediaStreams and DataChannels based on the
774 // local session description.
775 mediastream_signaling_->OnLocalDescriptionChanged(local_desc_.get());
776
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000777 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000778 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
779 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
780 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000782 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 }
784 return true;
785}
786
787bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
788 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000789 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000790 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000791
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000792 // Validate SDP.
793 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
794 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795 }
796
797 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000798 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799 if (action == kOffer && !CreateChannels(desc->description())) {
800 // TODO(mallinath) - Handle CreateChannel failure, as new local description
801 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000802 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000803 }
804
805 // Remove channel and transport proxies, if MediaContentDescription is
806 // rejected.
807 RemoveUnusedChannelsAndTransports(desc->description());
808
809 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
810 // is called.
811 set_remote_description(desc->description()->Copy());
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000812 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 return false;
814 }
815
816 // Update remote MediaStreams.
817 mediastream_signaling_->OnRemoteDescriptionChanged(desc);
818 if (local_description() && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000819 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 }
821
822 // Copy all saved candidates.
823 CopySavedCandidates(desc);
824 // We retain all received candidates.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000825 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
826 remote_desc_.get(), desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 // Check if this new SessionDescription contains new ice ufrag and password
828 // that indicates the remote peer requests ice restart.
829 ice_restart_latch_->CheckForRemoteIceRestart(remote_desc_.get(),
830 desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000831 remote_desc_.reset(desc_temp.release());
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000832
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000833 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000834 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
835 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
836 }
837
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000839 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840 }
841 return true;
842}
843
844bool WebRtcSession::UpdateSessionState(
845 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000846 std::string* err_desc) {
847 // If there's already a pending error then no state transition should happen.
848 // But all call-sites should be verifying this before calling us!
849 ASSERT(error() == cricket::BaseSession::ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000850 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000852 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
853 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854 }
855 SetState(source == cricket::CS_LOCAL ?
856 STATE_SENTINITIATE : STATE_RECEIVEDINITIATE);
857 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000858 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 }
860 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000861 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
862 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 }
864 EnableChannels();
865 SetState(source == cricket::CS_LOCAL ?
866 STATE_SENTPRACCEPT : STATE_RECEIVEDPRACCEPT);
867 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000868 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869 }
870 } else if (action == kAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000871 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
872 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873 }
874 MaybeEnableMuxingSupport();
875 EnableChannels();
876 SetState(source == cricket::CS_LOCAL ?
877 STATE_SENTACCEPT : STATE_RECEIVEDACCEPT);
878 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000879 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880 }
881 }
882 return true;
883}
884
885WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
886 if (type == SessionDescriptionInterface::kOffer) {
887 return WebRtcSession::kOffer;
888 } else if (type == SessionDescriptionInterface::kPrAnswer) {
889 return WebRtcSession::kPrAnswer;
890 } else if (type == SessionDescriptionInterface::kAnswer) {
891 return WebRtcSession::kAnswer;
892 }
893 ASSERT(false && "unknown action type");
894 return WebRtcSession::kOffer;
895}
896
897bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
898 if (state() == STATE_INIT) {
899 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
900 << "without any offer (local or remote) "
901 << "session description.";
902 return false;
903 }
904
905 if (!candidate) {
906 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL";
907 return false;
908 }
909
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +0000910 bool valid = false;
911 if (!ReadyToUseRemoteCandidate(candidate, NULL, &valid)) {
912 if (valid) {
913 LOG(LS_INFO) << "ProcessIceMessage: Candidate saved";
914 saved_candidates_.push_back(
915 new JsepIceCandidate(candidate->sdp_mid(),
916 candidate->sdp_mline_index(),
917 candidate->candidate()));
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000918 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +0000919 return valid;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 }
921
922 // Add this candidate to the remote session description.
923 if (!remote_desc_->AddCandidate(candidate)) {
924 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used";
925 return false;
926 }
927
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000928 return UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000929}
930
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000931bool WebRtcSession::SetIceTransports(
932 PeerConnectionInterface::IceTransportsType type) {
933 return port_allocator()->set_candidate_filter(
934 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000935}
936
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000937bool WebRtcSession::GetLocalTrackIdBySsrc(uint32 ssrc, std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938 if (!BaseSession::local_description())
939 return false;
940 return webrtc::GetTrackIdBySsrc(
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000941 BaseSession::local_description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942}
943
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000944bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32 ssrc, std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945 if (!BaseSession::remote_description())
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000946 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000947 return webrtc::GetTrackIdBySsrc(
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000948 BaseSession::remote_description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949}
950
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000951std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000952 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000953 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954 return desc.str();
955}
956
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000957void WebRtcSession::SetAudioPlayout(uint32 ssrc, bool enable,
958 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000959 ASSERT(signaling_thread()->IsCurrent());
960 if (!voice_channel_) {
961 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
962 return;
963 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000964 if (!voice_channel_->SetRemoteRenderer(ssrc, renderer)) {
965 // SetRenderer() can fail if the ssrc does not match any playout channel.
966 LOG(LS_ERROR) << "SetAudioPlayout: ssrc is incorrect: " << ssrc;
967 return;
968 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969 if (!voice_channel_->SetOutputScaling(ssrc, enable ? 1 : 0, enable ? 1 : 0)) {
970 // Allow that SetOutputScaling fail if |enable| is false but assert
971 // otherwise. This in the normal case when the underlying media channel has
972 // already been deleted.
973 ASSERT(enable == false);
974 }
975}
976
977void WebRtcSession::SetAudioSend(uint32 ssrc, bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000978 const cricket::AudioOptions& options,
979 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000980 ASSERT(signaling_thread()->IsCurrent());
981 if (!voice_channel_) {
982 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
983 return;
984 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000985 if (!voice_channel_->SetLocalRenderer(ssrc, renderer)) {
986 // SetRenderer() can fail if the ssrc does not match any send channel.
987 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
988 return;
989 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000990 if (!voice_channel_->MuteStream(ssrc, !enable)) {
991 // Allow that MuteStream fail if |enable| is false but assert otherwise.
992 // This in the normal case when the underlying media channel has already
993 // been deleted.
994 ASSERT(enable == false);
995 return;
996 }
997 if (enable)
998 voice_channel_->SetChannelOptions(options);
999}
1000
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001001void WebRtcSession::SetAudioPlayoutVolume(uint32 ssrc, double volume) {
1002 ASSERT(signaling_thread()->IsCurrent());
1003 ASSERT(volume >= 0 && volume <= 10);
1004 if (!voice_channel_) {
1005 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1006 return;
1007 }
1008
1009 if (!voice_channel_->SetOutputScaling(ssrc, volume, volume))
1010 ASSERT(false);
1011}
1012
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001013bool WebRtcSession::SetCaptureDevice(uint32 ssrc,
1014 cricket::VideoCapturer* camera) {
1015 ASSERT(signaling_thread()->IsCurrent());
1016
1017 if (!video_channel_.get()) {
1018 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1019 // support video.
1020 LOG(LS_WARNING) << "Video not used in this call.";
1021 return false;
1022 }
1023 if (!video_channel_->SetCapturer(ssrc, camera)) {
1024 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1025 // This in the normal case when the underlying media channel has already
1026 // been deleted.
1027 ASSERT(camera == NULL);
1028 return false;
1029 }
1030 return true;
1031}
1032
1033void WebRtcSession::SetVideoPlayout(uint32 ssrc,
1034 bool enable,
1035 cricket::VideoRenderer* renderer) {
1036 ASSERT(signaling_thread()->IsCurrent());
1037 if (!video_channel_) {
1038 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1039 return;
1040 }
1041 if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) {
1042 // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise.
1043 // This in the normal case when the underlying media channel has already
1044 // been deleted.
1045 ASSERT(renderer == NULL);
1046 }
1047}
1048
1049void WebRtcSession::SetVideoSend(uint32 ssrc, bool enable,
1050 const cricket::VideoOptions* options) {
1051 ASSERT(signaling_thread()->IsCurrent());
1052 if (!video_channel_) {
1053 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1054 return;
1055 }
1056 if (!video_channel_->MuteStream(ssrc, !enable)) {
1057 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1058 // This in the normal case when the underlying media channel has already
1059 // been deleted.
1060 ASSERT(enable == false);
1061 return;
1062 }
1063 if (enable && options)
1064 video_channel_->SetChannelOptions(*options);
1065}
1066
1067bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1068 ASSERT(signaling_thread()->IsCurrent());
1069 if (!voice_channel_) {
1070 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1071 return false;
1072 }
1073 uint32 send_ssrc = 0;
1074 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1075 // exists.
1076 if (!GetAudioSsrcByTrackId(BaseSession::local_description(), track_id,
1077 &send_ssrc)) {
1078 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1079 return false;
1080 }
1081 return voice_channel_->CanInsertDtmf();
1082}
1083
1084bool WebRtcSession::InsertDtmf(const std::string& track_id,
1085 int code, int duration) {
1086 ASSERT(signaling_thread()->IsCurrent());
1087 if (!voice_channel_) {
1088 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1089 return false;
1090 }
1091 uint32 send_ssrc = 0;
1092 if (!VERIFY(GetAudioSsrcByTrackId(BaseSession::local_description(),
1093 track_id, &send_ssrc))) {
1094 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1095 return false;
1096 }
1097 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration,
1098 cricket::DF_SEND)) {
1099 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1100 return false;
1101 }
1102 return true;
1103}
1104
1105sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
1106 return &SignalVoiceChannelDestroyed;
1107}
1108
wu@webrtc.org78187522013-10-07 23:32:02 +00001109bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001110 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001111 cricket::SendDataResult* result) {
1112 if (!data_channel_.get()) {
1113 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1114 return false;
1115 }
1116 return data_channel_->SendData(params, payload, result);
1117}
1118
1119bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
1120 if (!data_channel_.get()) {
1121 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1122 return false;
1123 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001124 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1125 &DataChannel::OnChannelReady);
1126 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1127 &DataChannel::OnDataReceived);
wu@webrtc.org78187522013-10-07 23:32:02 +00001128 return true;
1129}
1130
1131void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001132 if (!data_channel_.get()) {
1133 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1134 return;
1135 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001136 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1137 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
1138}
1139
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001140void WebRtcSession::AddSctpDataStream(int sid) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001141 if (!data_channel_.get()) {
1142 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1143 return;
1144 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001145 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1146 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001147}
1148
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001149void WebRtcSession::RemoveSctpDataStream(int sid) {
1150 mediastream_signaling_->RemoveSctpDataChannel(sid);
jiayl@webrtc.org2eaac182014-06-17 16:02:46 +00001151
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001152 if (!data_channel_.get()) {
1153 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1154 << "NULL.";
1155 return;
1156 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001157 data_channel_->RemoveRecvStream(sid);
1158 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001159}
1160
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001161bool WebRtcSession::ReadyToSendData() const {
1162 return data_channel_.get() && data_channel_->ready_to_send_data();
1163}
1164
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001165rtc::scoped_refptr<DataChannel> WebRtcSession::CreateDataChannel(
wu@webrtc.org78187522013-10-07 23:32:02 +00001166 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001167 const InternalDataChannelInit* config) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001168 if (state() == STATE_RECEIVEDTERMINATE) {
1169 return NULL;
1170 }
1171 if (data_channel_type_ == cricket::DCT_NONE) {
1172 LOG(LS_ERROR) << "CreateDataChannel: Data is not supported in this call.";
1173 return NULL;
1174 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001175 InternalDataChannelInit new_config =
1176 config ? (*config) : InternalDataChannelInit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001177 if (data_channel_type_ == cricket::DCT_SCTP) {
1178 if (new_config.id < 0) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001179 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001180 if (GetSslRole(&role) &&
1181 !mediastream_signaling_->AllocateSctpSid(role, &new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001182 LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel.";
1183 return NULL;
1184 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001185 } else if (!mediastream_signaling_->IsSctpSidAvailable(new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001186 LOG(LS_ERROR) << "Failed to create a SCTP data channel "
1187 << "because the id is already in use or out of range.";
1188 return NULL;
1189 }
1190 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001191
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001192 rtc::scoped_refptr<DataChannel> channel(DataChannel::Create(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001193 this, data_channel_type_, label, new_config));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001194 if (channel && !mediastream_signaling_->AddDataChannel(channel))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001195 return NULL;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001196
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001197 return channel;
1198}
1199
1200cricket::DataChannelType WebRtcSession::data_channel_type() const {
1201 return data_channel_type_;
1202}
1203
wu@webrtc.org91053e72013-08-10 07:18:04 +00001204bool WebRtcSession::IceRestartPending() const {
1205 return ice_restart_latch_->Get();
1206}
1207
1208void WebRtcSession::ResetIceRestartLatch() {
1209 ice_restart_latch_->Reset();
1210}
1211
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001212void WebRtcSession::OnIdentityReady(rtc::SSLIdentity* identity) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001213 SetIdentity(identity);
1214}
1215
1216bool WebRtcSession::waiting_for_identity() const {
1217 return webrtc_session_desc_factory_->waiting_for_identity();
1218}
1219
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001220void WebRtcSession::SetIceConnectionState(
1221 PeerConnectionInterface::IceConnectionState state) {
1222 if (ice_connection_state_ == state) {
1223 return;
1224 }
1225
1226 // ASSERT that the requested transition is allowed. Note that
1227 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1228 // within PeerConnection). This switch statement should compile away when
1229 // ASSERTs are disabled.
1230 switch (ice_connection_state_) {
1231 case PeerConnectionInterface::kIceConnectionNew:
1232 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1233 break;
1234 case PeerConnectionInterface::kIceConnectionChecking:
1235 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1236 state == PeerConnectionInterface::kIceConnectionConnected);
1237 break;
1238 case PeerConnectionInterface::kIceConnectionConnected:
1239 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1240 state == PeerConnectionInterface::kIceConnectionChecking ||
1241 state == PeerConnectionInterface::kIceConnectionCompleted);
1242 break;
1243 case PeerConnectionInterface::kIceConnectionCompleted:
1244 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1245 state == PeerConnectionInterface::kIceConnectionDisconnected);
1246 break;
1247 case PeerConnectionInterface::kIceConnectionFailed:
1248 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1249 break;
1250 case PeerConnectionInterface::kIceConnectionDisconnected:
1251 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1252 state == PeerConnectionInterface::kIceConnectionConnected ||
1253 state == PeerConnectionInterface::kIceConnectionCompleted ||
1254 state == PeerConnectionInterface::kIceConnectionFailed);
1255 break;
1256 case PeerConnectionInterface::kIceConnectionClosed:
1257 ASSERT(false);
1258 break;
1259 default:
1260 ASSERT(false);
1261 break;
1262 }
1263
1264 ice_connection_state_ = state;
1265 if (ice_observer_) {
1266 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1267 }
1268}
1269
1270void WebRtcSession::OnTransportRequestSignaling(
1271 cricket::Transport* transport) {
1272 ASSERT(signaling_thread()->IsCurrent());
1273 transport->OnSignalingReady();
1274 if (ice_observer_) {
1275 ice_observer_->OnIceGatheringChange(
1276 PeerConnectionInterface::kIceGatheringGathering);
1277 }
1278}
1279
1280void WebRtcSession::OnTransportConnecting(cricket::Transport* transport) {
1281 ASSERT(signaling_thread()->IsCurrent());
1282 // start monitoring for the write state of the transport.
1283 OnTransportWritable(transport);
1284}
1285
1286void WebRtcSession::OnTransportWritable(cricket::Transport* transport) {
1287 ASSERT(signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001288 if (transport->all_channels_writable()) {
henrike@webrtc.org05376342014-03-10 15:53:12 +00001289 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001290 } else if (transport->HasChannels()) {
1291 // If the current state is Connected or Completed, then there were writable
1292 // channels but now there are not, so the next state must be Disconnected.
1293 if (ice_connection_state_ ==
1294 PeerConnectionInterface::kIceConnectionConnected ||
1295 ice_connection_state_ ==
1296 PeerConnectionInterface::kIceConnectionCompleted) {
1297 SetIceConnectionState(
1298 PeerConnectionInterface::kIceConnectionDisconnected);
1299 }
1300 }
1301}
1302
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001303void WebRtcSession::OnTransportCompleted(cricket::Transport* transport) {
1304 ASSERT(signaling_thread()->IsCurrent());
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001305 PeerConnectionInterface::IceConnectionState old_state = ice_connection_state_;
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001306 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001307 // Only report once when Ice connection is completed.
1308 if (old_state != PeerConnectionInterface::kIceConnectionCompleted) {
1309 ReportBestConnectionState(transport);
1310 }
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001311}
1312
1313void WebRtcSession::OnTransportFailed(cricket::Transport* transport) {
1314 ASSERT(signaling_thread()->IsCurrent());
1315 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1316}
1317
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001318void WebRtcSession::OnTransportProxyCandidatesReady(
1319 cricket::TransportProxy* proxy, const cricket::Candidates& candidates) {
1320 ASSERT(signaling_thread()->IsCurrent());
1321 ProcessNewLocalCandidate(proxy->content_name(), candidates);
1322}
1323
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001324void WebRtcSession::OnCandidatesAllocationDone() {
1325 ASSERT(signaling_thread()->IsCurrent());
1326 if (ice_observer_) {
1327 ice_observer_->OnIceGatheringChange(
1328 PeerConnectionInterface::kIceGatheringComplete);
1329 ice_observer_->OnIceComplete();
1330 }
1331}
1332
1333// Enabling voice and video channel.
1334void WebRtcSession::EnableChannels() {
1335 if (voice_channel_ && !voice_channel_->enabled())
1336 voice_channel_->Enable(true);
1337
1338 if (video_channel_ && !video_channel_->enabled())
1339 video_channel_->Enable(true);
1340
1341 if (data_channel_.get() && !data_channel_->enabled())
1342 data_channel_->Enable(true);
1343}
1344
1345void WebRtcSession::ProcessNewLocalCandidate(
1346 const std::string& content_name,
1347 const cricket::Candidates& candidates) {
1348 int sdp_mline_index;
1349 if (!GetLocalCandidateMediaIndex(content_name, &sdp_mline_index)) {
1350 LOG(LS_ERROR) << "ProcessNewLocalCandidate: content name "
1351 << content_name << " not found";
1352 return;
1353 }
1354
1355 for (cricket::Candidates::const_iterator citer = candidates.begin();
1356 citer != candidates.end(); ++citer) {
1357 // Use content_name as the candidate media id.
1358 JsepIceCandidate candidate(content_name, sdp_mline_index, *citer);
1359 if (ice_observer_) {
1360 ice_observer_->OnIceCandidate(&candidate);
1361 }
1362 if (local_desc_) {
1363 local_desc_->AddCandidate(&candidate);
1364 }
1365 }
1366}
1367
1368// Returns the media index for a local ice candidate given the content name.
1369bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1370 int* sdp_mline_index) {
1371 if (!BaseSession::local_description() || !sdp_mline_index)
1372 return false;
1373
1374 bool content_found = false;
1375 const ContentInfos& contents = BaseSession::local_description()->contents();
1376 for (size_t index = 0; index < contents.size(); ++index) {
1377 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001378 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001379 content_found = true;
1380 break;
1381 }
1382 }
1383 return content_found;
1384}
1385
1386bool WebRtcSession::UseCandidatesInSessionDescription(
1387 const SessionDescriptionInterface* remote_desc) {
1388 if (!remote_desc)
1389 return true;
1390 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001391
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001392 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1393 const IceCandidateCollection* candidates = remote_desc->candidates(m);
1394 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001395 const IceCandidateInterface* candidate = candidates->at(n);
1396 bool valid = false;
1397 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1398 if (valid) {
1399 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Candidate saved.";
1400 saved_candidates_.push_back(
1401 new JsepIceCandidate(candidate->sdp_mid(),
1402 candidate->sdp_mline_index(),
1403 candidate->candidate()));
1404 }
1405 continue;
1406 }
1407
1408 ret = UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001409 if (!ret)
1410 break;
1411 }
1412 }
1413 return ret;
1414}
1415
1416bool WebRtcSession::UseCandidate(
1417 const IceCandidateInterface* candidate) {
1418
1419 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
1420 size_t remote_content_size =
1421 BaseSession::remote_description()->contents().size();
1422 if (mediacontent_index >= remote_content_size) {
1423 LOG(LS_ERROR)
1424 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1425 return false;
1426 }
1427
1428 cricket::ContentInfo content =
1429 BaseSession::remote_description()->contents()[mediacontent_index];
1430 std::vector<cricket::Candidate> candidates;
1431 candidates.push_back(candidate->candidate());
1432 // Invoking BaseSession method to handle remote candidates.
1433 std::string error;
1434 if (OnRemoteCandidates(content.name, candidates, &error)) {
1435 // Candidates successfully submitted for checking.
1436 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1437 ice_connection_state_ ==
1438 PeerConnectionInterface::kIceConnectionDisconnected) {
1439 // If state is New, then the session has just gotten its first remote ICE
1440 // candidates, so go to Checking.
1441 // If state is Disconnected, the session is re-using old candidates or
1442 // receiving additional ones, so go to Checking.
1443 // If state is Connected, stay Connected.
1444 // TODO(bemasc): If state is Connected, and the new candidates are for a
1445 // newly added transport, then the state actually _should_ move to
1446 // checking. Add a way to distinguish that case.
1447 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1448 }
1449 // TODO(bemasc): If state is Completed, go back to Connected.
1450 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001451 if (!error.empty()) {
1452 LOG(LS_WARNING) << error;
1453 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001454 }
1455 return true;
1456}
1457
1458void WebRtcSession::RemoveUnusedChannelsAndTransports(
1459 const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001460 // Destroy video_channel_ first since it may have a pointer to the
1461 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001462 const cricket::ContentInfo* video_info =
1463 cricket::GetFirstVideoContent(desc);
1464 if ((!video_info || video_info->rejected) && video_channel_) {
1465 mediastream_signaling_->OnVideoChannelClose();
1466 SignalVideoChannelDestroyed();
1467 const std::string content_name = video_channel_->content_name();
1468 channel_manager_->DestroyVideoChannel(video_channel_.release());
1469 DestroyTransportProxy(content_name);
1470 }
1471
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001472 const cricket::ContentInfo* voice_info =
1473 cricket::GetFirstAudioContent(desc);
1474 if ((!voice_info || voice_info->rejected) && voice_channel_) {
1475 mediastream_signaling_->OnAudioChannelClose();
1476 SignalVoiceChannelDestroyed();
1477 const std::string content_name = voice_channel_->content_name();
1478 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
1479 DestroyTransportProxy(content_name);
1480 }
1481
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001482 const cricket::ContentInfo* data_info =
1483 cricket::GetFirstDataContent(desc);
1484 if ((!data_info || data_info->rejected) && data_channel_) {
1485 mediastream_signaling_->OnDataChannelClose();
1486 SignalDataChannelDestroyed();
1487 const std::string content_name = data_channel_->content_name();
1488 channel_manager_->DestroyDataChannel(data_channel_.release());
1489 DestroyTransportProxy(content_name);
1490 }
1491}
1492
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001493// TODO(mallinath) - Add a correct error code if the channels are not creatued
1494// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001495bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
1496 // Disabling the BUNDLE flag in PortAllocator if offer disabled it.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001497 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1498 if (state() == STATE_INIT && !bundle_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001499 port_allocator()->set_flags(port_allocator()->flags() &
1500 ~cricket::PORTALLOCATOR_ENABLE_BUNDLE);
1501 }
1502
1503 // Creating the media channels and transport proxies.
1504 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1505 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001506 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001507 LOG(LS_ERROR) << "Failed to create voice channel.";
1508 return false;
1509 }
1510 }
1511
1512 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1513 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001514 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001515 LOG(LS_ERROR) << "Failed to create video channel.";
1516 return false;
1517 }
1518 }
1519
1520 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1521 if (data_channel_type_ != cricket::DCT_NONE &&
1522 data && !data->rejected && !data_channel_.get()) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001523 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001524 LOG(LS_ERROR) << "Failed to create data channel.";
1525 return false;
1526 }
1527 }
1528
1529 return true;
1530}
1531
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001532bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001533 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001534 this, content->name, true));
wu@webrtc.orgde305012013-10-31 15:40:38 +00001535 if (!voice_channel_.get())
1536 return false;
1537
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00001538 voice_channel_->SetChannelOptions(audio_options_);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001539 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001540}
1541
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001542bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001543 video_channel_.reset(channel_manager_->CreateVideoChannel(
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +00001544 this, content->name, true, video_options_, voice_channel_.get()));
1545 return video_channel_.get() != NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001546}
1547
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001548bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001549 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001550 data_channel_.reset(channel_manager_->CreateDataChannel(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001551 this, content->name, !sctp, data_channel_type_));
wu@webrtc.org91053e72013-08-10 07:18:04 +00001552 if (!data_channel_.get()) {
1553 return false;
1554 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001555 if (sctp) {
1556 mediastream_signaling_->OnDataTransportCreatedForSctp();
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001557 data_channel_->SignalDataReceived.connect(
1558 this, &WebRtcSession::OnDataChannelMessageReceived);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00001559 data_channel_->SignalStreamClosedRemotely.connect(
1560 mediastream_signaling_,
1561 &MediaStreamSignaling::OnRemoteSctpDataChannelClosed);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001562 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001563 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001564}
1565
1566void WebRtcSession::CopySavedCandidates(
1567 SessionDescriptionInterface* dest_desc) {
1568 if (!dest_desc) {
1569 ASSERT(false);
1570 return;
1571 }
1572 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
1573 dest_desc->AddCandidate(saved_candidates_[i]);
1574 delete saved_candidates_[i];
1575 }
1576 saved_candidates_.clear();
1577}
1578
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001579void WebRtcSession::OnDataChannelMessageReceived(
1580 cricket::DataChannel* channel,
1581 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001582 const rtc::Buffer& payload) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001583 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001584 if (params.type == cricket::DMT_CONTROL &&
1585 mediastream_signaling_->IsSctpSidAvailable(params.ssrc)) {
1586 // Received CONTROL on unused sid, process as an OPEN message.
1587 mediastream_signaling_->AddDataChannelFromOpenMessage(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001588 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001589 // otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001590}
1591
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001592// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001593bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001594 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1595 if (!bundle_enabled)
1596 return true;
1597
1598 const cricket::ContentGroup* bundle_group =
1599 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1600 ASSERT(bundle_group != NULL);
1601
1602 const cricket::ContentInfos& contents = desc->contents();
1603 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1604 citer != contents.end(); ++citer) {
1605 const cricket::ContentInfo* content = (&*citer);
1606 ASSERT(content != NULL);
1607 if (bundle_group->HasContentName(content->name) &&
1608 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1609 if (!HasRtcpMuxEnabled(content))
1610 return false;
1611 }
1612 }
1613 // RTCP-MUX is enabled in all the contents.
1614 return true;
1615}
1616
1617bool WebRtcSession::HasRtcpMuxEnabled(
1618 const cricket::ContentInfo* content) {
1619 const cricket::MediaContentDescription* description =
1620 static_cast<cricket::MediaContentDescription*>(content->description);
1621 return description->rtcp_mux();
1622}
1623
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001624bool WebRtcSession::ValidateSessionDescription(
1625 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001626 cricket::ContentSource source, std::string* err_desc) {
1627 std::string type;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001628 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001629 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001630 }
1631
1632 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001633 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001634 }
1635
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001636 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001637 Action action = GetAction(sdesc->type());
1638 if (source == cricket::CS_LOCAL) {
1639 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001640 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001641 } else {
1642 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001643 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001644 }
1645
1646 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001647 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001648 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1649 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001650 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001651 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001652 }
1653
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001654 // Verify ice-ufrag and ice-pwd.
1655 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001656 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001657 }
1658
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001659 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001660 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001661 }
1662
1663 // Verify m-lines in Answer when compared against Offer.
1664 if (action == kAnswer) {
1665 const cricket::SessionDescription* offer_desc =
1666 (source == cricket::CS_LOCAL) ? remote_description()->description() :
1667 local_description()->description();
1668 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001669 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001670 }
1671 }
1672
1673 return true;
1674}
1675
1676bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1677 return ((action == kOffer && state() == STATE_INIT) ||
1678 // update local offer
1679 (action == kOffer && state() == STATE_SENTINITIATE) ||
1680 // update the current ongoing session.
1681 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1682 (action == kOffer && state() == STATE_SENTACCEPT) ||
1683 (action == kOffer && state() == STATE_INPROGRESS) ||
1684 // accept remote offer
1685 (action == kAnswer && state() == STATE_RECEIVEDINITIATE) ||
1686 (action == kAnswer && state() == STATE_SENTPRACCEPT) ||
1687 (action == kPrAnswer && state() == STATE_RECEIVEDINITIATE) ||
1688 (action == kPrAnswer && state() == STATE_SENTPRACCEPT));
1689}
1690
1691bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1692 return ((action == kOffer && state() == STATE_INIT) ||
1693 // update remote offer
1694 (action == kOffer && state() == STATE_RECEIVEDINITIATE) ||
1695 // update the current ongoing session
1696 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1697 (action == kOffer && state() == STATE_SENTACCEPT) ||
1698 (action == kOffer && state() == STATE_INPROGRESS) ||
1699 // accept local offer
1700 (action == kAnswer && state() == STATE_SENTINITIATE) ||
1701 (action == kAnswer && state() == STATE_RECEIVEDPRACCEPT) ||
1702 (action == kPrAnswer && state() == STATE_SENTINITIATE) ||
1703 (action == kPrAnswer && state() == STATE_RECEIVEDPRACCEPT));
1704}
1705
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001706std::string WebRtcSession::GetSessionErrorMsg() {
1707 std::ostringstream desc;
1708 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1709 desc << kSessionErrorDesc << error_desc() << ".";
1710 return desc.str();
1711}
1712
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001713// We need to check the local/remote description for the Transport instead of
1714// the session, because a new Transport added during renegotiation may have
1715// them unset while the session has them set from the previous negotiation.
1716// Not doing so may trigger the auto generation of transport description and
1717// mess up DTLS identity information, ICE credential, etc.
1718bool WebRtcSession::ReadyToUseRemoteCandidate(
1719 const IceCandidateInterface* candidate,
1720 const SessionDescriptionInterface* remote_desc,
1721 bool* valid) {
1722 *valid = true;;
1723 cricket::TransportProxy* transport_proxy = NULL;
1724
1725 const SessionDescriptionInterface* current_remote_desc =
1726 remote_desc ? remote_desc : remote_description();
1727
1728 if (!current_remote_desc)
1729 return false;
1730
1731 size_t mediacontent_index =
1732 static_cast<size_t>(candidate->sdp_mline_index());
1733 size_t remote_content_size =
1734 current_remote_desc->description()->contents().size();
1735 if (mediacontent_index >= remote_content_size) {
1736 LOG(LS_ERROR)
1737 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
1738
1739 *valid = false;
1740 return false;
1741 }
1742
1743 cricket::ContentInfo content =
1744 current_remote_desc->description()->contents()[mediacontent_index];
1745 transport_proxy = GetTransportProxy(content.name);
1746
1747 return transport_proxy && transport_proxy->local_description_set() &&
1748 transport_proxy->remote_description_set();
1749}
1750
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001751// Walk through the ConnectionInfos to gather best connection usage
1752// for IPv4 and IPv6.
1753void WebRtcSession::ReportBestConnectionState(cricket::Transport* transport) {
1754 if (!metrics_observer_) {
1755 return;
1756 }
1757
1758 cricket::TransportStats stats;
1759 if (!transport->GetStats(&stats)) {
1760 return;
1761 }
1762
1763 for (cricket::TransportChannelStatsList::const_iterator it =
1764 stats.channel_stats.begin();
1765 it != stats.channel_stats.end(); ++it) {
1766 for (cricket::ConnectionInfos::const_iterator it_info =
1767 it->connection_infos.begin();
1768 it_info != it->connection_infos.end(); ++it_info) {
1769 if (!it_info->best_connection) {
1770 continue;
1771 }
1772 if (it_info->local_candidate.address().family() == AF_INET) {
1773 metrics_observer_->IncrementCounter(kBestConnections_IPv4);
1774 } else if (it_info->local_candidate.address().family() ==
1775 AF_INET6) {
1776 metrics_observer_->IncrementCounter(kBestConnections_IPv6);
1777 } else {
1778 ASSERT(false);
1779 }
1780 return;
1781 }
1782 }
1783}
1784
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785} // namespace webrtc