blob: 5935ea0464214ed1eb348e1614d5287e4c6066df [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
30#include <algorithm>
31#include <climits>
32#include <vector>
33
34#include "talk/app/webrtc/jsepicecandidate.h"
35#include "talk/app/webrtc/jsepsessiondescription.h"
36#include "talk/app/webrtc/mediaconstraintsinterface.h"
37#include "talk/app/webrtc/mediastreamsignaling.h"
38#include "talk/app/webrtc/peerconnectioninterface.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000039#include "talk/app/webrtc/webrtcsessiondescriptionfactory.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040#include "talk/base/helpers.h"
41#include "talk/base/logging.h"
42#include "talk/base/stringencode.h"
43#include "talk/media/base/constants.h"
44#include "talk/media/base/videocapturer.h"
45#include "talk/session/media/channel.h"
46#include "talk/session/media/channelmanager.h"
47#include "talk/session/media/mediasession.h"
48
49using cricket::ContentInfo;
50using cricket::ContentInfos;
51using cricket::MediaContentDescription;
52using cricket::SessionDescription;
53using cricket::TransportInfo;
54
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055namespace webrtc {
56
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000057const char MediaConstraintsInterface::kInternalConstraintPrefix[] = "internal";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058
59// Supported MediaConstraints.
wu@webrtc.orgde305012013-10-31 15:40:38 +000060// DSCP constraints.
61const char MediaConstraintsInterface::kEnableDscp[] = "googDscp";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062// DTLS-SRTP pseudo-constraints.
63const char MediaConstraintsInterface::kEnableDtlsSrtp[] =
64 "DtlsSrtpKeyAgreement";
65// DataChannel pseudo constraints.
66const char MediaConstraintsInterface::kEnableRtpDataChannels[] =
67 "RtpDataChannels";
68// This constraint is for internal use only, representing the Chrome command
69// line flag. So it is prefixed with kInternalConstraintPrefix so JS values
70// will be removed.
71const char MediaConstraintsInterface::kEnableSctpDataChannels[] =
wu@webrtc.org97077a32013-10-25 21:18:33 +000072 "deprecatedSctpDataChannels";
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +000073
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074// Error messages
75const char kSetLocalSdpFailed[] = "SetLocalDescription failed: ";
76const char kSetRemoteSdpFailed[] = "SetRemoteDescription failed: ";
77const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000078const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
79 "is enabled.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080const char kInvalidCandidates[] = "Description contains invalid candidates.";
81const char kInvalidSdp[] = "Invalid session description.";
82const char kMlineMismatch[] =
83 "Offer and answer descriptions m-lines are not matching. "
84 "Rejecting answer.";
85const char kSdpWithoutCrypto[] = "Called with a SDP without crypto enabled.";
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000086const char kSdpWithoutSdesAndDtlsDisabled[] =
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000087 "Called with an SDP without SDES crypto and DTLS disabled locally.";
88const char kSdpWithoutIceUfragPwd[] =
89 "Called with an SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090const char kSessionError[] = "Session error code: ";
91const char kUpdateStateFailed[] = "Failed to update session state: ";
92const char kPushDownOfferTDFailed[] =
93 "Failed to push down offer transport description.";
94const char kPushDownPranswerTDFailed[] =
95 "Failed to push down pranswer transport description.";
96const char kPushDownAnswerTDFailed[] =
97 "Failed to push down answer transport description.";
98
99// Compares |answer| against |offer|. Comparision is done
100// for number of m-lines in answer against offer. If matches true will be
101// returned otherwise false.
102static bool VerifyMediaDescriptions(
103 const SessionDescription* answer, const SessionDescription* offer) {
104 if (offer->contents().size() != answer->contents().size())
105 return false;
106
107 for (size_t i = 0; i < offer->contents().size(); ++i) {
108 if ((offer->contents()[i].name) != answer->contents()[i].name) {
109 return false;
110 }
111 }
112 return true;
113}
114
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115// Checks that each non-rejected content has SDES crypto keys or a DTLS
116// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
117// keys, will be caught in Transport negotiation, and backstopped by Channel's
118// |secure_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000119static bool VerifyCrypto(const SessionDescription* desc,
120 bool dtls_enabled,
121 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122 const ContentInfos& contents = desc->contents();
123 for (size_t index = 0; index < contents.size(); ++index) {
124 const ContentInfo* cinfo = &contents[index];
125 if (cinfo->rejected) {
126 continue;
127 }
128
129 // If the content isn't rejected, crypto must be present.
130 const MediaContentDescription* media =
131 static_cast<const MediaContentDescription*>(cinfo->description);
132 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
133 if (!media || !tinfo) {
134 // Something is not right.
135 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000136 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137 return false;
138 }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000139 if (media->cryptos().empty()) {
140 if (!tinfo->description.identity_fingerprint) {
141 // Crypto must be supplied.
142 LOG(LS_WARNING) << "Session description must have SDES or DTLS-SRTP.";
143 *error = kSdpWithoutCrypto;
144 return false;
145 }
146 if (!dtls_enabled) {
147 LOG(LS_WARNING) <<
148 "Session description must have SDES when DTLS disabled.";
149 *error = kSdpWithoutSdesAndDtlsDisabled;
150 return false;
151 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152 }
153 }
154
155 return true;
156}
157
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000158// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
159static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
160 const ContentInfos& contents = desc->contents();
161 for (size_t index = 0; index < contents.size(); ++index) {
162 const ContentInfo* cinfo = &contents[index];
163 if (cinfo->rejected) {
164 continue;
165 }
166
167 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
168 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
169 if (!tinfo) {
170 // Something is not right.
171 LOG(LS_ERROR) << kInvalidSdp;
172 return false;
173 }
174 if (tinfo->description.ice_ufrag.empty() ||
175 tinfo->description.ice_pwd.empty()) {
176 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
177 return false;
178 }
179 }
180 return true;
181}
182
wu@webrtc.org91053e72013-08-10 07:18:04 +0000183// Forces |sdesc->crypto_required| to the appropriate state based on the
184// current security policy, to ensure a failure occurs if there is an error
185// in crypto negotiation.
186// Called when processing the local session description.
187static void UpdateSessionDescriptionSecurePolicy(
188 cricket::SecureMediaPolicy secure_policy,
189 SessionDescription* sdesc) {
190 if (!sdesc) {
191 return;
192 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193
wu@webrtc.org91053e72013-08-10 07:18:04 +0000194 // Updating the |crypto_required_| in MediaContentDescription to the
195 // appropriate state based on the current security policy.
196 for (cricket::ContentInfos::iterator iter = sdesc->contents().begin();
197 iter != sdesc->contents().end(); ++iter) {
198 if (cricket::IsMediaContent(&*iter)) {
199 MediaContentDescription* mdesc =
200 static_cast<MediaContentDescription*> (iter->description);
201 if (mdesc) {
202 mdesc->set_crypto_required(secure_policy == cricket::SEC_REQUIRED);
203 }
204 }
205 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206}
207
208static bool GetAudioSsrcByTrackId(
209 const SessionDescription* session_description,
210 const std::string& track_id, uint32 *ssrc) {
211 const cricket::ContentInfo* audio_info =
212 cricket::GetFirstAudioContent(session_description);
213 if (!audio_info) {
214 LOG(LS_ERROR) << "Audio not used in this call";
215 return false;
216 }
217
218 const cricket::MediaContentDescription* audio_content =
219 static_cast<const cricket::MediaContentDescription*>(
220 audio_info->description);
221 cricket::StreamParams stream;
222 if (!cricket::GetStreamByIds(audio_content->streams(), "", track_id,
223 &stream)) {
224 return false;
225 }
226 *ssrc = stream.first_ssrc();
227 return true;
228}
229
230static bool GetTrackIdBySsrc(const SessionDescription* session_description,
231 uint32 ssrc, std::string* track_id) {
232 ASSERT(track_id != NULL);
233
234 cricket::StreamParams stream_out;
235 const cricket::ContentInfo* audio_info =
236 cricket::GetFirstAudioContent(session_description);
237 if (!audio_info) {
238 return false;
239 }
240 const cricket::MediaContentDescription* audio_content =
241 static_cast<const cricket::MediaContentDescription*>(
242 audio_info->description);
243
244 if (cricket::GetStreamBySsrc(audio_content->streams(), ssrc, &stream_out)) {
245 *track_id = stream_out.id;
246 return true;
247 }
248
249 const cricket::ContentInfo* video_info =
250 cricket::GetFirstVideoContent(session_description);
251 if (!video_info) {
252 return false;
253 }
254 const cricket::MediaContentDescription* video_content =
255 static_cast<const cricket::MediaContentDescription*>(
256 video_info->description);
257
258 if (cricket::GetStreamBySsrc(video_content->streams(), ssrc, &stream_out)) {
259 *track_id = stream_out.id;
260 return true;
261 }
262 return false;
263}
264
265static bool BadSdp(const std::string& desc, std::string* err_desc) {
266 if (err_desc) {
267 *err_desc = desc;
268 }
269 LOG(LS_ERROR) << desc;
270 return false;
271}
272
273static bool BadLocalSdp(const std::string& desc, std::string* err_desc) {
274 std::string set_local_sdp_failed = kSetLocalSdpFailed;
275 set_local_sdp_failed.append(desc);
276 return BadSdp(set_local_sdp_failed, err_desc);
277}
278
279static bool BadRemoteSdp(const std::string& desc, std::string* err_desc) {
280 std::string set_remote_sdp_failed = kSetRemoteSdpFailed;
281 set_remote_sdp_failed.append(desc);
282 return BadSdp(set_remote_sdp_failed, err_desc);
283}
284
285static bool BadSdp(cricket::ContentSource source,
286 const std::string& desc, std::string* err_desc) {
287 if (source == cricket::CS_LOCAL) {
288 return BadLocalSdp(desc, err_desc);
289 } else {
290 return BadRemoteSdp(desc, err_desc);
291 }
292}
293
294static std::string SessionErrorMsg(cricket::BaseSession::Error error) {
295 std::ostringstream desc;
296 desc << kSessionError << error;
297 return desc.str();
298}
299
300#define GET_STRING_OF_STATE(state) \
301 case cricket::BaseSession::state: \
302 result = #state; \
303 break;
304
305static std::string GetStateString(cricket::BaseSession::State state) {
306 std::string result;
307 switch (state) {
308 GET_STRING_OF_STATE(STATE_INIT)
309 GET_STRING_OF_STATE(STATE_SENTINITIATE)
310 GET_STRING_OF_STATE(STATE_RECEIVEDINITIATE)
311 GET_STRING_OF_STATE(STATE_SENTPRACCEPT)
312 GET_STRING_OF_STATE(STATE_SENTACCEPT)
313 GET_STRING_OF_STATE(STATE_RECEIVEDPRACCEPT)
314 GET_STRING_OF_STATE(STATE_RECEIVEDACCEPT)
315 GET_STRING_OF_STATE(STATE_SENTMODIFY)
316 GET_STRING_OF_STATE(STATE_RECEIVEDMODIFY)
317 GET_STRING_OF_STATE(STATE_SENTREJECT)
318 GET_STRING_OF_STATE(STATE_RECEIVEDREJECT)
319 GET_STRING_OF_STATE(STATE_SENTREDIRECT)
320 GET_STRING_OF_STATE(STATE_SENTTERMINATE)
321 GET_STRING_OF_STATE(STATE_RECEIVEDTERMINATE)
322 GET_STRING_OF_STATE(STATE_INPROGRESS)
323 GET_STRING_OF_STATE(STATE_DEINIT)
324 default:
325 ASSERT(false);
326 break;
327 }
328 return result;
329}
330
331#define GET_STRING_OF_ERROR(err) \
332 case cricket::BaseSession::err: \
333 result = #err; \
334 break;
335
336static std::string GetErrorString(cricket::BaseSession::Error err) {
337 std::string result;
338 switch (err) {
339 GET_STRING_OF_ERROR(ERROR_NONE)
340 GET_STRING_OF_ERROR(ERROR_TIME)
341 GET_STRING_OF_ERROR(ERROR_RESPONSE)
342 GET_STRING_OF_ERROR(ERROR_NETWORK)
343 GET_STRING_OF_ERROR(ERROR_CONTENT)
344 GET_STRING_OF_ERROR(ERROR_TRANSPORT)
345 default:
346 ASSERT(false);
347 break;
348 }
349 return result;
350}
351
352static bool SetSessionStateFailed(cricket::ContentSource source,
353 cricket::BaseSession::Error err,
354 std::string* err_desc) {
355 std::string set_state_err = kUpdateStateFailed;
356 set_state_err.append(GetErrorString(err));
357 return BadSdp(source, set_state_err, err_desc);
358}
359
360// Help class used to remember if a a remote peer has requested ice restart by
361// by sending a description with new ice ufrag and password.
362class IceRestartAnswerLatch {
363 public:
364 IceRestartAnswerLatch() : ice_restart_(false) { }
365
wu@webrtc.org91053e72013-08-10 07:18:04 +0000366 // Returns true if CheckForRemoteIceRestart has been called with a new session
367 // description where ice password and ufrag has changed since last time
368 // Reset() was called.
369 bool Get() const {
370 return ice_restart_;
371 }
372
373 void Reset() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000374 if (ice_restart_) {
375 ice_restart_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000377 }
378
379 void CheckForRemoteIceRestart(
380 const SessionDescriptionInterface* old_desc,
381 const SessionDescriptionInterface* new_desc) {
382 if (!old_desc || new_desc->type() != SessionDescriptionInterface::kOffer) {
383 return;
384 }
385 const SessionDescription* new_sd = new_desc->description();
386 const SessionDescription* old_sd = old_desc->description();
387 const ContentInfos& contents = new_sd->contents();
388 for (size_t index = 0; index < contents.size(); ++index) {
389 const ContentInfo* cinfo = &contents[index];
390 if (cinfo->rejected) {
391 continue;
392 }
393 // If the content isn't rejected, check if ufrag and password has
394 // changed.
395 const cricket::TransportDescription* new_transport_desc =
396 new_sd->GetTransportDescriptionByName(cinfo->name);
397 const cricket::TransportDescription* old_transport_desc =
398 old_sd->GetTransportDescriptionByName(cinfo->name);
399 if (!new_transport_desc || !old_transport_desc) {
400 // No transport description exist. This is not an ice restart.
401 continue;
402 }
403 if (new_transport_desc->ice_pwd != old_transport_desc->ice_pwd &&
404 new_transport_desc->ice_ufrag != old_transport_desc->ice_ufrag) {
405 LOG(LS_INFO) << "Remote peer request ice restart.";
406 ice_restart_ = true;
407 break;
408 }
409 }
410 }
411
412 private:
413 bool ice_restart_;
414};
415
wu@webrtc.org91053e72013-08-10 07:18:04 +0000416WebRtcSession::WebRtcSession(
417 cricket::ChannelManager* channel_manager,
418 talk_base::Thread* signaling_thread,
419 talk_base::Thread* worker_thread,
420 cricket::PortAllocator* port_allocator,
421 MediaStreamSignaling* mediastream_signaling)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422 : cricket::BaseSession(signaling_thread, worker_thread, port_allocator,
423 talk_base::ToString(talk_base::CreateRandomId64() &
424 LLONG_MAX),
425 cricket::NS_JINGLE_RTP, false),
426 // RFC 3264: The numeric value of the session id and version in the
427 // o line MUST be representable with a "64 bit signed integer".
428 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
429 channel_manager_(channel_manager),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430 mediastream_signaling_(mediastream_signaling),
431 ice_observer_(NULL),
432 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000433 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000434 dtls_enabled_(false),
wu@webrtc.orgde305012013-10-31 15:40:38 +0000435 dscp_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436 data_channel_type_(cricket::DCT_NONE),
437 ice_restart_latch_(new IceRestartAnswerLatch) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000438}
439
440WebRtcSession::~WebRtcSession() {
441 if (voice_channel_.get()) {
442 SignalVoiceChannelDestroyed();
443 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
444 }
445 if (video_channel_.get()) {
446 SignalVideoChannelDestroyed();
447 channel_manager_->DestroyVideoChannel(video_channel_.release());
448 }
449 if (data_channel_.get()) {
450 SignalDataChannelDestroyed();
451 channel_manager_->DestroyDataChannel(data_channel_.release());
452 }
453 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
454 delete saved_candidates_[i];
455 }
456 delete identity();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457}
458
wu@webrtc.org91053e72013-08-10 07:18:04 +0000459bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000460 const PeerConnectionFactoryInterface::Options& options,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000461 const MediaConstraintsInterface* constraints,
462 DTLSIdentityServiceInterface* dtls_identity_service) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463 // TODO(perkj): Take |constraints| into consideration. Return false if not all
464 // mandatory constraints can be fulfilled. Note that |constraints|
465 // can be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466 bool value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000467
468 // Enable DTLS by default if |dtls_identity_service| is valid.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000469 dtls_enabled_ = (dtls_identity_service != NULL);
470 // |constraints| can override the default |dtls_enabled_| value.
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000471 if (FindConstraint(
472 constraints,
473 MediaConstraintsInterface::kEnableDtlsSrtp,
474 &value, NULL)) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000475 dtls_enabled_ = value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000476 }
477
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000478 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000479 // It takes precendence over the disable_sctp_data_channels
480 // PeerConnectionFactoryInterface::Options.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481 if (FindConstraint(
482 constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
483 &value, NULL) && value) {
484 LOG(LS_INFO) << "Allowing RTP data engine.";
485 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000486 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000487 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000488 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000489 LOG(LS_INFO) << "Allowing SCTP data engine.";
490 data_channel_type_ = cricket::DCT_SCTP;
491 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492 }
493 if (data_channel_type_ != cricket::DCT_NONE) {
494 mediastream_signaling_->SetDataChannelFactory(this);
495 }
496
wu@webrtc.orgde305012013-10-31 15:40:38 +0000497 // Find DSCP constraint.
498 if (FindConstraint(
499 constraints,
500 MediaConstraintsInterface::kEnableDscp,
501 &value, NULL)) {
502 dscp_enabled_ = value;
503 }
504
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505 const cricket::VideoCodec default_codec(
506 JsepSessionDescription::kDefaultVideoCodecId,
507 JsepSessionDescription::kDefaultVideoCodecName,
508 JsepSessionDescription::kMaxVideoCodecWidth,
509 JsepSessionDescription::kMaxVideoCodecHeight,
510 JsepSessionDescription::kDefaultVideoCodecFramerate,
511 JsepSessionDescription::kDefaultVideoCodecPreference);
512 channel_manager_->SetDefaultVideoEncoderConfig(
513 cricket::VideoEncoderConfig(default_codec));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000514
515 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
516 signaling_thread(),
517 channel_manager_,
518 mediastream_signaling_,
519 dtls_identity_service,
520 this,
521 id(),
522 data_channel_type_,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000523 dtls_enabled_));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000524
525 webrtc_session_desc_factory_->SignalIdentityReady.connect(
526 this, &WebRtcSession::OnIdentityReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000527
wu@webrtc.org97077a32013-10-25 21:18:33 +0000528 if (options.disable_encryption) {
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000529 webrtc_session_desc_factory_->set_secure(cricket::SEC_DISABLED);
530 }
531
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532 return true;
533}
534
535void WebRtcSession::Terminate() {
536 SetState(STATE_RECEIVEDTERMINATE);
537 RemoveUnusedChannelsAndTransports(NULL);
538 ASSERT(voice_channel_.get() == NULL);
539 ASSERT(video_channel_.get() == NULL);
540 ASSERT(data_channel_.get() == NULL);
541}
542
543bool WebRtcSession::StartCandidatesAllocation() {
544 // SpeculativelyConnectTransportChannels, will call ConnectChannels method
545 // from TransportProxy to start gathering ice candidates.
546 SpeculativelyConnectAllTransportChannels();
547 if (!saved_candidates_.empty()) {
548 // If there are saved candidates which arrived before local description is
549 // set, copy those to remote description.
550 CopySavedCandidates(remote_desc_.get());
551 }
552 // Push remote candidates present in remote description to transport channels.
553 UseCandidatesInSessionDescription(remote_desc_.get());
554 return true;
555}
556
557void WebRtcSession::set_secure_policy(
558 cricket::SecureMediaPolicy secure_policy) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000559 webrtc_session_desc_factory_->set_secure(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000560}
561
wu@webrtc.org91053e72013-08-10 07:18:04 +0000562cricket::SecureMediaPolicy WebRtcSession::secure_policy() const {
563 return webrtc_session_desc_factory_->secure();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564}
565
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000566bool WebRtcSession::GetSslRole(talk_base::SSLRole* role) {
567 if (local_description() == NULL || remote_description() == NULL) {
568 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
569 << "SSL Role of the session.";
570 return false;
571 }
572
573 // TODO(mallinath) - Return role of each transport, as role may differ from
574 // one another.
575 // In current implementaion we just return the role of first transport in the
576 // transport map.
577 for (cricket::TransportMap::const_iterator iter = transport_proxies().begin();
578 iter != transport_proxies().end(); ++iter) {
579 if (iter->second->impl()) {
580 return iter->second->impl()->GetSslRole(role);
581 }
582 }
583 return false;
584}
585
wu@webrtc.org91053e72013-08-10 07:18:04 +0000586void WebRtcSession::CreateOffer(CreateSessionDescriptionObserver* observer,
587 const MediaConstraintsInterface* constraints) {
588 webrtc_session_desc_factory_->CreateOffer(observer, constraints);
589}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000590
wu@webrtc.org91053e72013-08-10 07:18:04 +0000591void WebRtcSession::CreateAnswer(CreateSessionDescriptionObserver* observer,
592 const MediaConstraintsInterface* constraints) {
593 webrtc_session_desc_factory_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000594}
595
596bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
597 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000598 // Takes the ownership of |desc| regardless of the result.
599 talk_base::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
600
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000601 // Validate SDP.
602 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
603 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604 }
605
606 // Update the initiator flag if this session is the initiator.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000607 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 if (state() == STATE_INIT && action == kOffer) {
609 set_initiator(true);
610 }
611
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000612 cricket::SecureMediaPolicy secure_policy =
613 webrtc_session_desc_factory_->secure();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 // Update the MediaContentDescription crypto settings as per the policy set.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000615 UpdateSessionDescriptionSecurePolicy(secure_policy, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616
617 set_local_description(desc->description()->Copy());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000618 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000619
620 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000621 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622 // TODO(mallinath) - Handle CreateChannel failure, as new local description
623 // is applied. Restore back to old description.
624 return BadLocalSdp(kCreateChannelFailed, err_desc);
625 }
626
627 // Remove channel and transport proxies, if MediaContentDescription is
628 // rejected.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000629 RemoveUnusedChannelsAndTransports(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630
631 if (!UpdateSessionState(action, cricket::CS_LOCAL,
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000632 local_desc_->description(), err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633 return false;
634 }
635 // Kick starting the ice candidates allocation.
636 StartCandidatesAllocation();
637
638 // Update state and SSRC of local MediaStreams and DataChannels based on the
639 // local session description.
640 mediastream_signaling_->OnLocalDescriptionChanged(local_desc_.get());
641
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000642 talk_base::SSLRole role;
643 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
644 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
645 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000646 if (error() != cricket::BaseSession::ERROR_NONE) {
647 return BadLocalSdp(SessionErrorMsg(error()), err_desc);
648 }
649 return true;
650}
651
652bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
653 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000654 // Takes the ownership of |desc| regardless of the result.
655 talk_base::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
656
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000657 // Validate SDP.
658 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
659 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000660 }
661
662 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000663 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000664 if (action == kOffer && !CreateChannels(desc->description())) {
665 // TODO(mallinath) - Handle CreateChannel failure, as new local description
666 // is applied. Restore back to old description.
667 return BadRemoteSdp(kCreateChannelFailed, err_desc);
668 }
669
670 // Remove channel and transport proxies, if MediaContentDescription is
671 // rejected.
672 RemoveUnusedChannelsAndTransports(desc->description());
673
674 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
675 // is called.
676 set_remote_description(desc->description()->Copy());
677 if (!UpdateSessionState(action, cricket::CS_REMOTE,
678 desc->description(), err_desc)) {
679 return false;
680 }
681
682 // Update remote MediaStreams.
683 mediastream_signaling_->OnRemoteDescriptionChanged(desc);
684 if (local_description() && !UseCandidatesInSessionDescription(desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685 return BadRemoteSdp(kInvalidCandidates, err_desc);
686 }
687
688 // Copy all saved candidates.
689 CopySavedCandidates(desc);
690 // We retain all received candidates.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000691 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
692 remote_desc_.get(), desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000693 // Check if this new SessionDescription contains new ice ufrag and password
694 // that indicates the remote peer requests ice restart.
695 ice_restart_latch_->CheckForRemoteIceRestart(remote_desc_.get(),
696 desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000697 remote_desc_.reset(desc_temp.release());
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000698
699 talk_base::SSLRole role;
700 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
701 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
702 }
703
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000704 if (error() != cricket::BaseSession::ERROR_NONE) {
705 return BadRemoteSdp(SessionErrorMsg(error()), err_desc);
706 }
707 return true;
708}
709
710bool WebRtcSession::UpdateSessionState(
711 Action action, cricket::ContentSource source,
712 const cricket::SessionDescription* desc,
713 std::string* err_desc) {
714 // If there's already a pending error then no state transition should happen.
715 // But all call-sites should be verifying this before calling us!
716 ASSERT(error() == cricket::BaseSession::ERROR_NONE);
717 if (action == kOffer) {
718 if (!PushdownTransportDescription(source, cricket::CA_OFFER)) {
719 return BadSdp(source, kPushDownOfferTDFailed, err_desc);
720 }
721 SetState(source == cricket::CS_LOCAL ?
722 STATE_SENTINITIATE : STATE_RECEIVEDINITIATE);
723 if (error() != cricket::BaseSession::ERROR_NONE) {
724 return SetSessionStateFailed(source, error(), err_desc);
725 }
726 } else if (action == kPrAnswer) {
727 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER)) {
728 return BadSdp(source, kPushDownPranswerTDFailed, err_desc);
729 }
730 EnableChannels();
731 SetState(source == cricket::CS_LOCAL ?
732 STATE_SENTPRACCEPT : STATE_RECEIVEDPRACCEPT);
733 if (error() != cricket::BaseSession::ERROR_NONE) {
734 return SetSessionStateFailed(source, error(), err_desc);
735 }
736 } else if (action == kAnswer) {
737 if (!PushdownTransportDescription(source, cricket::CA_ANSWER)) {
738 return BadSdp(source, kPushDownAnswerTDFailed, err_desc);
739 }
740 MaybeEnableMuxingSupport();
741 EnableChannels();
742 SetState(source == cricket::CS_LOCAL ?
743 STATE_SENTACCEPT : STATE_RECEIVEDACCEPT);
744 if (error() != cricket::BaseSession::ERROR_NONE) {
745 return SetSessionStateFailed(source, error(), err_desc);
746 }
747 }
748 return true;
749}
750
751WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
752 if (type == SessionDescriptionInterface::kOffer) {
753 return WebRtcSession::kOffer;
754 } else if (type == SessionDescriptionInterface::kPrAnswer) {
755 return WebRtcSession::kPrAnswer;
756 } else if (type == SessionDescriptionInterface::kAnswer) {
757 return WebRtcSession::kAnswer;
758 }
759 ASSERT(false && "unknown action type");
760 return WebRtcSession::kOffer;
761}
762
763bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
764 if (state() == STATE_INIT) {
765 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
766 << "without any offer (local or remote) "
767 << "session description.";
768 return false;
769 }
770
771 if (!candidate) {
772 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL";
773 return false;
774 }
775
776 if (!local_description() || !remote_description()) {
777 LOG(LS_INFO) << "ProcessIceMessage: Remote description not set, "
778 << "save the candidate for later use.";
779 saved_candidates_.push_back(
780 new JsepIceCandidate(candidate->sdp_mid(), candidate->sdp_mline_index(),
781 candidate->candidate()));
782 return true;
783 }
784
785 // Add this candidate to the remote session description.
786 if (!remote_desc_->AddCandidate(candidate)) {
787 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used";
788 return false;
789 }
790
791 return UseCandidatesInSessionDescription(remote_desc_.get());
792}
793
794bool WebRtcSession::GetTrackIdBySsrc(uint32 ssrc, std::string* id) {
795 if (GetLocalTrackId(ssrc, id)) {
796 if (GetRemoteTrackId(ssrc, id)) {
797 LOG(LS_WARNING) << "SSRC " << ssrc
798 << " exists in both local and remote descriptions";
799 return true; // We return the remote track id.
800 }
801 return true;
802 } else {
803 return GetRemoteTrackId(ssrc, id);
804 }
805}
806
807bool WebRtcSession::GetLocalTrackId(uint32 ssrc, std::string* track_id) {
808 if (!BaseSession::local_description())
809 return false;
810 return webrtc::GetTrackIdBySsrc(
811 BaseSession::local_description(), ssrc, track_id);
812}
813
814bool WebRtcSession::GetRemoteTrackId(uint32 ssrc, std::string* track_id) {
815 if (!BaseSession::remote_description())
816 return false;
817 return webrtc::GetTrackIdBySsrc(
818 BaseSession::remote_description(), ssrc, track_id);
819}
820
821std::string WebRtcSession::BadStateErrMsg(
822 const std::string& type, State state) {
823 std::ostringstream desc;
824 desc << "Called with type in wrong state, "
825 << "type: " << type << " state: " << GetStateString(state);
826 return desc.str();
827}
828
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000829void WebRtcSession::SetAudioPlayout(uint32 ssrc, bool enable,
830 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000831 ASSERT(signaling_thread()->IsCurrent());
832 if (!voice_channel_) {
833 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
834 return;
835 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000836 if (!voice_channel_->SetRemoteRenderer(ssrc, renderer)) {
837 // SetRenderer() can fail if the ssrc does not match any playout channel.
838 LOG(LS_ERROR) << "SetAudioPlayout: ssrc is incorrect: " << ssrc;
839 return;
840 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841 if (!voice_channel_->SetOutputScaling(ssrc, enable ? 1 : 0, enable ? 1 : 0)) {
842 // Allow that SetOutputScaling fail if |enable| is false but assert
843 // otherwise. This in the normal case when the underlying media channel has
844 // already been deleted.
845 ASSERT(enable == false);
846 }
847}
848
849void WebRtcSession::SetAudioSend(uint32 ssrc, bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000850 const cricket::AudioOptions& options,
851 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852 ASSERT(signaling_thread()->IsCurrent());
853 if (!voice_channel_) {
854 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
855 return;
856 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000857 if (!voice_channel_->SetLocalRenderer(ssrc, renderer)) {
858 // SetRenderer() can fail if the ssrc does not match any send channel.
859 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
860 return;
861 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 if (!voice_channel_->MuteStream(ssrc, !enable)) {
863 // Allow that MuteStream fail if |enable| is false but assert otherwise.
864 // This in the normal case when the underlying media channel has already
865 // been deleted.
866 ASSERT(enable == false);
867 return;
868 }
869 if (enable)
870 voice_channel_->SetChannelOptions(options);
871}
872
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873bool WebRtcSession::SetCaptureDevice(uint32 ssrc,
874 cricket::VideoCapturer* camera) {
875 ASSERT(signaling_thread()->IsCurrent());
876
877 if (!video_channel_.get()) {
878 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
879 // support video.
880 LOG(LS_WARNING) << "Video not used in this call.";
881 return false;
882 }
883 if (!video_channel_->SetCapturer(ssrc, camera)) {
884 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
885 // This in the normal case when the underlying media channel has already
886 // been deleted.
887 ASSERT(camera == NULL);
888 return false;
889 }
890 return true;
891}
892
893void WebRtcSession::SetVideoPlayout(uint32 ssrc,
894 bool enable,
895 cricket::VideoRenderer* renderer) {
896 ASSERT(signaling_thread()->IsCurrent());
897 if (!video_channel_) {
898 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
899 return;
900 }
901 if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) {
902 // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise.
903 // This in the normal case when the underlying media channel has already
904 // been deleted.
905 ASSERT(renderer == NULL);
906 }
907}
908
909void WebRtcSession::SetVideoSend(uint32 ssrc, bool enable,
910 const cricket::VideoOptions* options) {
911 ASSERT(signaling_thread()->IsCurrent());
912 if (!video_channel_) {
913 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
914 return;
915 }
916 if (!video_channel_->MuteStream(ssrc, !enable)) {
917 // Allow that MuteStream fail if |enable| is false but assert otherwise.
918 // This in the normal case when the underlying media channel has already
919 // been deleted.
920 ASSERT(enable == false);
921 return;
922 }
923 if (enable && options)
924 video_channel_->SetChannelOptions(*options);
925}
926
927bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
928 ASSERT(signaling_thread()->IsCurrent());
929 if (!voice_channel_) {
930 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
931 return false;
932 }
933 uint32 send_ssrc = 0;
934 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
935 // exists.
936 if (!GetAudioSsrcByTrackId(BaseSession::local_description(), track_id,
937 &send_ssrc)) {
938 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
939 return false;
940 }
941 return voice_channel_->CanInsertDtmf();
942}
943
944bool WebRtcSession::InsertDtmf(const std::string& track_id,
945 int code, int duration) {
946 ASSERT(signaling_thread()->IsCurrent());
947 if (!voice_channel_) {
948 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
949 return false;
950 }
951 uint32 send_ssrc = 0;
952 if (!VERIFY(GetAudioSsrcByTrackId(BaseSession::local_description(),
953 track_id, &send_ssrc))) {
954 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
955 return false;
956 }
957 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration,
958 cricket::DF_SEND)) {
959 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
960 return false;
961 }
962 return true;
963}
964
965sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
966 return &SignalVoiceChannelDestroyed;
967}
968
wu@webrtc.org78187522013-10-07 23:32:02 +0000969bool WebRtcSession::SendData(const cricket::SendDataParams& params,
970 const talk_base::Buffer& payload,
971 cricket::SendDataResult* result) {
972 if (!data_channel_.get()) {
973 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
974 return false;
975 }
976 return data_channel_->SendData(params, payload, result);
977}
978
979bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
980 if (!data_channel_.get()) {
981 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
982 return false;
983 }
wu@webrtc.org78187522013-10-07 23:32:02 +0000984 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
985 &DataChannel::OnChannelReady);
986 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
987 &DataChannel::OnDataReceived);
wu@webrtc.org78187522013-10-07 23:32:02 +0000988 return true;
989}
990
991void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000992 if (!data_channel_.get()) {
993 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
994 return;
995 }
wu@webrtc.org78187522013-10-07 23:32:02 +0000996 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
997 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
998}
999
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001000void WebRtcSession::AddRtpDataStream(uint32 send_ssrc, uint32 recv_ssrc) {
1001 if (!data_channel_.get()) {
1002 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1003 return;
1004 }
1005 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(recv_ssrc));
1006 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(send_ssrc));
1007}
1008
1009void WebRtcSession::AddSctpDataStream(uint32 sid) {
1010 AddRtpDataStream(sid, sid);
1011}
1012
1013void WebRtcSession::RemoveRtpDataStream(uint32 send_ssrc, uint32 recv_ssrc) {
1014 if (!data_channel_.get()) {
1015 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1016 << "NULL.";
1017 return;
1018 }
1019 data_channel_->RemoveRecvStream(recv_ssrc);
1020 data_channel_->RemoveSendStream(send_ssrc);
1021}
1022
1023void WebRtcSession::RemoveSctpDataStream(uint32 sid) {
1024 RemoveRtpDataStream(sid, sid);
1025}
1026
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001027bool WebRtcSession::ReadyToSendData() const {
1028 return data_channel_.get() && data_channel_->ready_to_send_data();
1029}
1030
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001031talk_base::scoped_refptr<DataChannel> WebRtcSession::CreateDataChannel(
wu@webrtc.org78187522013-10-07 23:32:02 +00001032 const std::string& label,
1033 const DataChannelInit* config) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001034 if (state() == STATE_RECEIVEDTERMINATE) {
1035 return NULL;
1036 }
1037 if (data_channel_type_ == cricket::DCT_NONE) {
1038 LOG(LS_ERROR) << "CreateDataChannel: Data is not supported in this call.";
1039 return NULL;
1040 }
1041 DataChannelInit new_config = config ? (*config) : DataChannelInit();
1042
1043 if (data_channel_type_ == cricket::DCT_SCTP) {
1044 if (new_config.id < 0) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001045 talk_base::SSLRole role;
1046 if (GetSslRole(&role) &&
1047 !mediastream_signaling_->AllocateSctpSid(role, &new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001048 LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel.";
1049 return NULL;
1050 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001051 } else if (!mediastream_signaling_->IsSctpSidAvailable(new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001052 LOG(LS_ERROR) << "Failed to create a SCTP data channel "
1053 << "because the id is already in use or out of range.";
1054 return NULL;
1055 }
1056 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001057
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001058 talk_base::scoped_refptr<DataChannel> channel(
wu@webrtc.org78187522013-10-07 23:32:02 +00001059 DataChannel::Create(this, data_channel_type_, label, &new_config));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001060 if (channel && !mediastream_signaling_->AddDataChannel(channel))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001061 return NULL;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001062
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001063 return channel;
1064}
1065
1066cricket::DataChannelType WebRtcSession::data_channel_type() const {
1067 return data_channel_type_;
1068}
1069
wu@webrtc.org91053e72013-08-10 07:18:04 +00001070bool WebRtcSession::IceRestartPending() const {
1071 return ice_restart_latch_->Get();
1072}
1073
1074void WebRtcSession::ResetIceRestartLatch() {
1075 ice_restart_latch_->Reset();
1076}
1077
1078void WebRtcSession::OnIdentityReady(talk_base::SSLIdentity* identity) {
1079 SetIdentity(identity);
1080}
1081
1082bool WebRtcSession::waiting_for_identity() const {
1083 return webrtc_session_desc_factory_->waiting_for_identity();
1084}
1085
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001086void WebRtcSession::SetIceConnectionState(
1087 PeerConnectionInterface::IceConnectionState state) {
1088 if (ice_connection_state_ == state) {
1089 return;
1090 }
1091
1092 // ASSERT that the requested transition is allowed. Note that
1093 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1094 // within PeerConnection). This switch statement should compile away when
1095 // ASSERTs are disabled.
1096 switch (ice_connection_state_) {
1097 case PeerConnectionInterface::kIceConnectionNew:
1098 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1099 break;
1100 case PeerConnectionInterface::kIceConnectionChecking:
1101 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1102 state == PeerConnectionInterface::kIceConnectionConnected);
1103 break;
1104 case PeerConnectionInterface::kIceConnectionConnected:
1105 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1106 state == PeerConnectionInterface::kIceConnectionChecking ||
1107 state == PeerConnectionInterface::kIceConnectionCompleted);
1108 break;
1109 case PeerConnectionInterface::kIceConnectionCompleted:
1110 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1111 state == PeerConnectionInterface::kIceConnectionDisconnected);
1112 break;
1113 case PeerConnectionInterface::kIceConnectionFailed:
1114 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1115 break;
1116 case PeerConnectionInterface::kIceConnectionDisconnected:
1117 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1118 state == PeerConnectionInterface::kIceConnectionConnected ||
1119 state == PeerConnectionInterface::kIceConnectionCompleted ||
1120 state == PeerConnectionInterface::kIceConnectionFailed);
1121 break;
1122 case PeerConnectionInterface::kIceConnectionClosed:
1123 ASSERT(false);
1124 break;
1125 default:
1126 ASSERT(false);
1127 break;
1128 }
1129
1130 ice_connection_state_ = state;
1131 if (ice_observer_) {
1132 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1133 }
1134}
1135
1136void WebRtcSession::OnTransportRequestSignaling(
1137 cricket::Transport* transport) {
1138 ASSERT(signaling_thread()->IsCurrent());
1139 transport->OnSignalingReady();
1140 if (ice_observer_) {
1141 ice_observer_->OnIceGatheringChange(
1142 PeerConnectionInterface::kIceGatheringGathering);
1143 }
1144}
1145
1146void WebRtcSession::OnTransportConnecting(cricket::Transport* transport) {
1147 ASSERT(signaling_thread()->IsCurrent());
1148 // start monitoring for the write state of the transport.
1149 OnTransportWritable(transport);
1150}
1151
1152void WebRtcSession::OnTransportWritable(cricket::Transport* transport) {
1153 ASSERT(signaling_thread()->IsCurrent());
1154 // TODO(bemasc): Expose more API from Transport to detect when
1155 // candidate selection starts or stops, due to success or failure.
1156 if (transport->all_channels_writable()) {
1157 if (ice_connection_state_ ==
1158 PeerConnectionInterface::kIceConnectionChecking ||
1159 ice_connection_state_ ==
1160 PeerConnectionInterface::kIceConnectionDisconnected) {
1161 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1162 }
1163 } else if (transport->HasChannels()) {
1164 // If the current state is Connected or Completed, then there were writable
1165 // channels but now there are not, so the next state must be Disconnected.
1166 if (ice_connection_state_ ==
1167 PeerConnectionInterface::kIceConnectionConnected ||
1168 ice_connection_state_ ==
1169 PeerConnectionInterface::kIceConnectionCompleted) {
1170 SetIceConnectionState(
1171 PeerConnectionInterface::kIceConnectionDisconnected);
1172 }
1173 }
1174}
1175
1176void WebRtcSession::OnTransportProxyCandidatesReady(
1177 cricket::TransportProxy* proxy, const cricket::Candidates& candidates) {
1178 ASSERT(signaling_thread()->IsCurrent());
1179 ProcessNewLocalCandidate(proxy->content_name(), candidates);
1180}
1181
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001182void WebRtcSession::OnCandidatesAllocationDone() {
1183 ASSERT(signaling_thread()->IsCurrent());
1184 if (ice_observer_) {
1185 ice_observer_->OnIceGatheringChange(
1186 PeerConnectionInterface::kIceGatheringComplete);
1187 ice_observer_->OnIceComplete();
1188 }
1189}
1190
1191// Enabling voice and video channel.
1192void WebRtcSession::EnableChannels() {
1193 if (voice_channel_ && !voice_channel_->enabled())
1194 voice_channel_->Enable(true);
1195
1196 if (video_channel_ && !video_channel_->enabled())
1197 video_channel_->Enable(true);
1198
1199 if (data_channel_.get() && !data_channel_->enabled())
1200 data_channel_->Enable(true);
1201}
1202
1203void WebRtcSession::ProcessNewLocalCandidate(
1204 const std::string& content_name,
1205 const cricket::Candidates& candidates) {
1206 int sdp_mline_index;
1207 if (!GetLocalCandidateMediaIndex(content_name, &sdp_mline_index)) {
1208 LOG(LS_ERROR) << "ProcessNewLocalCandidate: content name "
1209 << content_name << " not found";
1210 return;
1211 }
1212
1213 for (cricket::Candidates::const_iterator citer = candidates.begin();
1214 citer != candidates.end(); ++citer) {
1215 // Use content_name as the candidate media id.
1216 JsepIceCandidate candidate(content_name, sdp_mline_index, *citer);
1217 if (ice_observer_) {
1218 ice_observer_->OnIceCandidate(&candidate);
1219 }
1220 if (local_desc_) {
1221 local_desc_->AddCandidate(&candidate);
1222 }
1223 }
1224}
1225
1226// Returns the media index for a local ice candidate given the content name.
1227bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1228 int* sdp_mline_index) {
1229 if (!BaseSession::local_description() || !sdp_mline_index)
1230 return false;
1231
1232 bool content_found = false;
1233 const ContentInfos& contents = BaseSession::local_description()->contents();
1234 for (size_t index = 0; index < contents.size(); ++index) {
1235 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001236 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001237 content_found = true;
1238 break;
1239 }
1240 }
1241 return content_found;
1242}
1243
1244bool WebRtcSession::UseCandidatesInSessionDescription(
1245 const SessionDescriptionInterface* remote_desc) {
1246 if (!remote_desc)
1247 return true;
1248 bool ret = true;
1249 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1250 const IceCandidateCollection* candidates = remote_desc->candidates(m);
1251 for (size_t n = 0; n < candidates->count(); ++n) {
1252 ret = UseCandidate(candidates->at(n));
1253 if (!ret)
1254 break;
1255 }
1256 }
1257 return ret;
1258}
1259
1260bool WebRtcSession::UseCandidate(
1261 const IceCandidateInterface* candidate) {
1262
1263 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
1264 size_t remote_content_size =
1265 BaseSession::remote_description()->contents().size();
1266 if (mediacontent_index >= remote_content_size) {
1267 LOG(LS_ERROR)
1268 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1269 return false;
1270 }
1271
1272 cricket::ContentInfo content =
1273 BaseSession::remote_description()->contents()[mediacontent_index];
1274 std::vector<cricket::Candidate> candidates;
1275 candidates.push_back(candidate->candidate());
1276 // Invoking BaseSession method to handle remote candidates.
1277 std::string error;
1278 if (OnRemoteCandidates(content.name, candidates, &error)) {
1279 // Candidates successfully submitted for checking.
1280 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1281 ice_connection_state_ ==
1282 PeerConnectionInterface::kIceConnectionDisconnected) {
1283 // If state is New, then the session has just gotten its first remote ICE
1284 // candidates, so go to Checking.
1285 // If state is Disconnected, the session is re-using old candidates or
1286 // receiving additional ones, so go to Checking.
1287 // If state is Connected, stay Connected.
1288 // TODO(bemasc): If state is Connected, and the new candidates are for a
1289 // newly added transport, then the state actually _should_ move to
1290 // checking. Add a way to distinguish that case.
1291 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1292 }
1293 // TODO(bemasc): If state is Completed, go back to Connected.
1294 } else {
1295 LOG(LS_WARNING) << error;
1296 }
1297 return true;
1298}
1299
1300void WebRtcSession::RemoveUnusedChannelsAndTransports(
1301 const SessionDescription* desc) {
1302 const cricket::ContentInfo* voice_info =
1303 cricket::GetFirstAudioContent(desc);
1304 if ((!voice_info || voice_info->rejected) && voice_channel_) {
1305 mediastream_signaling_->OnAudioChannelClose();
1306 SignalVoiceChannelDestroyed();
1307 const std::string content_name = voice_channel_->content_name();
1308 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
1309 DestroyTransportProxy(content_name);
1310 }
1311
1312 const cricket::ContentInfo* video_info =
1313 cricket::GetFirstVideoContent(desc);
1314 if ((!video_info || video_info->rejected) && video_channel_) {
1315 mediastream_signaling_->OnVideoChannelClose();
1316 SignalVideoChannelDestroyed();
1317 const std::string content_name = video_channel_->content_name();
1318 channel_manager_->DestroyVideoChannel(video_channel_.release());
1319 DestroyTransportProxy(content_name);
1320 }
1321
1322 const cricket::ContentInfo* data_info =
1323 cricket::GetFirstDataContent(desc);
1324 if ((!data_info || data_info->rejected) && data_channel_) {
1325 mediastream_signaling_->OnDataChannelClose();
1326 SignalDataChannelDestroyed();
1327 const std::string content_name = data_channel_->content_name();
1328 channel_manager_->DestroyDataChannel(data_channel_.release());
1329 DestroyTransportProxy(content_name);
1330 }
1331}
1332
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001333// TODO(mallinath) - Add a correct error code if the channels are not creatued
1334// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001335bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
1336 // Disabling the BUNDLE flag in PortAllocator if offer disabled it.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001337 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1338 if (state() == STATE_INIT && !bundle_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001339 port_allocator()->set_flags(port_allocator()->flags() &
1340 ~cricket::PORTALLOCATOR_ENABLE_BUNDLE);
1341 }
1342
1343 // Creating the media channels and transport proxies.
1344 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1345 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001346 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001347 LOG(LS_ERROR) << "Failed to create voice channel.";
1348 return false;
1349 }
1350 }
1351
1352 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1353 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001354 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001355 LOG(LS_ERROR) << "Failed to create video channel.";
1356 return false;
1357 }
1358 }
1359
1360 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1361 if (data_channel_type_ != cricket::DCT_NONE &&
1362 data && !data->rejected && !data_channel_.get()) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001363 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001364 LOG(LS_ERROR) << "Failed to create data channel.";
1365 return false;
1366 }
1367 }
1368
1369 return true;
1370}
1371
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001372bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001373 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001374 this, content->name, true));
wu@webrtc.orgde305012013-10-31 15:40:38 +00001375 if (!voice_channel_.get())
1376 return false;
1377
1378 if (dscp_enabled_) {
1379 cricket::AudioOptions options;
1380 options.dscp.Set(true);
1381 voice_channel_->SetChannelOptions(options);
1382 }
1383 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001384}
1385
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001386bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001387 video_channel_.reset(channel_manager_->CreateVideoChannel(
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001388 this, content->name, true, voice_channel_.get()));
wu@webrtc.orgde305012013-10-31 15:40:38 +00001389 if (!video_channel_.get())
1390 return false;
1391
1392 if (dscp_enabled_) {
1393 cricket::VideoOptions options;
1394 options.dscp.Set(true);
1395 video_channel_->SetChannelOptions(options);
1396 }
1397 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001398}
1399
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001400bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001401 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001402 data_channel_.reset(channel_manager_->CreateDataChannel(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001403 this, content->name, !sctp, data_channel_type_));
wu@webrtc.org91053e72013-08-10 07:18:04 +00001404 if (!data_channel_.get()) {
1405 return false;
1406 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001407 if (sctp) {
1408 mediastream_signaling_->OnDataTransportCreatedForSctp();
1409 data_channel_->SignalNewStreamReceived.connect(
1410 this, &WebRtcSession::OnNewDataChannelReceived);
1411 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001412 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001413}
1414
1415void WebRtcSession::CopySavedCandidates(
1416 SessionDescriptionInterface* dest_desc) {
1417 if (!dest_desc) {
1418 ASSERT(false);
1419 return;
1420 }
1421 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
1422 dest_desc->AddCandidate(saved_candidates_[i]);
1423 delete saved_candidates_[i];
1424 }
1425 saved_candidates_.clear();
1426}
1427
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001428void WebRtcSession::OnNewDataChannelReceived(
1429 const std::string& label, const DataChannelInit& init) {
1430 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001431 if (!mediastream_signaling_->AddDataChannelFromOpenMessage(
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001432 label, init)) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001433 LOG(LS_WARNING) << "Failed to create data channel from OPEN message.";
1434 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001435 }
1436}
1437
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001438// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001439bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001440 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1441 if (!bundle_enabled)
1442 return true;
1443
1444 const cricket::ContentGroup* bundle_group =
1445 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1446 ASSERT(bundle_group != NULL);
1447
1448 const cricket::ContentInfos& contents = desc->contents();
1449 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1450 citer != contents.end(); ++citer) {
1451 const cricket::ContentInfo* content = (&*citer);
1452 ASSERT(content != NULL);
1453 if (bundle_group->HasContentName(content->name) &&
1454 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1455 if (!HasRtcpMuxEnabled(content))
1456 return false;
1457 }
1458 }
1459 // RTCP-MUX is enabled in all the contents.
1460 return true;
1461}
1462
1463bool WebRtcSession::HasRtcpMuxEnabled(
1464 const cricket::ContentInfo* content) {
1465 const cricket::MediaContentDescription* description =
1466 static_cast<cricket::MediaContentDescription*>(content->description);
1467 return description->rtcp_mux();
1468}
1469
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001470bool WebRtcSession::ValidateSessionDescription(
1471 const SessionDescriptionInterface* sdesc,
1472 cricket::ContentSource source, std::string* error_desc) {
1473
1474 if (error() != cricket::BaseSession::ERROR_NONE) {
1475 return BadSdp(source, SessionErrorMsg(error()), error_desc);
1476 }
1477
1478 if (!sdesc || !sdesc->description()) {
1479 return BadSdp(source, kInvalidSdp, error_desc);
1480 }
1481
1482 std::string type = sdesc->type();
1483 Action action = GetAction(sdesc->type());
1484 if (source == cricket::CS_LOCAL) {
1485 if (!ExpectSetLocalDescription(action))
1486 return BadSdp(source, BadStateErrMsg(type, state()), error_desc);
1487 } else {
1488 if (!ExpectSetRemoteDescription(action))
1489 return BadSdp(source, BadStateErrMsg(type, state()), error_desc);
1490 }
1491
1492 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001493 std::string crypto_error;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001494 if (webrtc_session_desc_factory_->secure() == cricket::SEC_REQUIRED &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001495 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
1496 return BadSdp(source, crypto_error, error_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001497 }
1498
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001499 // Verify ice-ufrag and ice-pwd.
1500 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
1501 return BadSdp(source, kSdpWithoutIceUfragPwd, error_desc);
1502 }
1503
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001504 if (!ValidateBundleSettings(sdesc->description())) {
1505 return BadSdp(source, kBundleWithoutRtcpMux, error_desc);
1506 }
1507
1508 // Verify m-lines in Answer when compared against Offer.
1509 if (action == kAnswer) {
1510 const cricket::SessionDescription* offer_desc =
1511 (source == cricket::CS_LOCAL) ? remote_description()->description() :
1512 local_description()->description();
1513 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
1514 return BadSdp(source, kMlineMismatch, error_desc);
1515 }
1516 }
1517
1518 return true;
1519}
1520
1521bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1522 return ((action == kOffer && state() == STATE_INIT) ||
1523 // update local offer
1524 (action == kOffer && state() == STATE_SENTINITIATE) ||
1525 // update the current ongoing session.
1526 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1527 (action == kOffer && state() == STATE_SENTACCEPT) ||
1528 (action == kOffer && state() == STATE_INPROGRESS) ||
1529 // accept remote offer
1530 (action == kAnswer && state() == STATE_RECEIVEDINITIATE) ||
1531 (action == kAnswer && state() == STATE_SENTPRACCEPT) ||
1532 (action == kPrAnswer && state() == STATE_RECEIVEDINITIATE) ||
1533 (action == kPrAnswer && state() == STATE_SENTPRACCEPT));
1534}
1535
1536bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1537 return ((action == kOffer && state() == STATE_INIT) ||
1538 // update remote offer
1539 (action == kOffer && state() == STATE_RECEIVEDINITIATE) ||
1540 // update the current ongoing session
1541 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1542 (action == kOffer && state() == STATE_SENTACCEPT) ||
1543 (action == kOffer && state() == STATE_INPROGRESS) ||
1544 // accept local offer
1545 (action == kAnswer && state() == STATE_SENTINITIATE) ||
1546 (action == kAnswer && state() == STATE_RECEIVEDPRACCEPT) ||
1547 (action == kPrAnswer && state() == STATE_SENTINITIATE) ||
1548 (action == kPrAnswer && state() == STATE_RECEIVEDPRACCEPT));
1549}
1550
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001551} // namespace webrtc