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