blob: 404d963995651fa13bc047c512ce92fe6ddec1fe [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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057// Error messages
58const char kSetLocalSdpFailed[] = "SetLocalDescription failed: ";
59const char kSetRemoteSdpFailed[] = "SetRemoteDescription failed: ";
60const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000061const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
62 "is enabled.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063const char kInvalidCandidates[] = "Description contains invalid candidates.";
64const char kInvalidSdp[] = "Invalid session description.";
65const char kMlineMismatch[] =
66 "Offer and answer descriptions m-lines are not matching. "
67 "Rejecting answer.";
68const char kSdpWithoutCrypto[] = "Called with a SDP without crypto enabled.";
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000069const char kSdpWithoutSdesAndDtlsDisabled[] =
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000070 "Called with an SDP without SDES crypto and DTLS disabled locally.";
71const char kSdpWithoutIceUfragPwd[] =
72 "Called with an SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073const char kSessionError[] = "Session error code: ";
74const char kUpdateStateFailed[] = "Failed to update session state: ";
75const char kPushDownOfferTDFailed[] =
76 "Failed to push down offer transport description.";
77const char kPushDownPranswerTDFailed[] =
78 "Failed to push down pranswer transport description.";
79const char kPushDownAnswerTDFailed[] =
80 "Failed to push down answer transport description.";
81
82// Compares |answer| against |offer|. Comparision is done
83// for number of m-lines in answer against offer. If matches true will be
84// returned otherwise false.
85static bool VerifyMediaDescriptions(
86 const SessionDescription* answer, const SessionDescription* offer) {
87 if (offer->contents().size() != answer->contents().size())
88 return false;
89
90 for (size_t i = 0; i < offer->contents().size(); ++i) {
91 if ((offer->contents()[i].name) != answer->contents()[i].name) {
92 return false;
93 }
94 }
95 return true;
96}
97
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098// Checks that each non-rejected content has SDES crypto keys or a DTLS
99// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
100// keys, will be caught in Transport negotiation, and backstopped by Channel's
101// |secure_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000102static bool VerifyCrypto(const SessionDescription* desc,
103 bool dtls_enabled,
104 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 const ContentInfos& contents = desc->contents();
106 for (size_t index = 0; index < contents.size(); ++index) {
107 const ContentInfo* cinfo = &contents[index];
108 if (cinfo->rejected) {
109 continue;
110 }
111
112 // If the content isn't rejected, crypto must be present.
113 const MediaContentDescription* media =
114 static_cast<const MediaContentDescription*>(cinfo->description);
115 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
116 if (!media || !tinfo) {
117 // Something is not right.
118 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000119 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120 return false;
121 }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000122 if (media->cryptos().empty()) {
123 if (!tinfo->description.identity_fingerprint) {
124 // Crypto must be supplied.
125 LOG(LS_WARNING) << "Session description must have SDES or DTLS-SRTP.";
126 *error = kSdpWithoutCrypto;
127 return false;
128 }
129 if (!dtls_enabled) {
130 LOG(LS_WARNING) <<
131 "Session description must have SDES when DTLS disabled.";
132 *error = kSdpWithoutSdesAndDtlsDisabled;
133 return false;
134 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135 }
136 }
137
138 return true;
139}
140
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000141// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
142static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
143 const ContentInfos& contents = desc->contents();
144 for (size_t index = 0; index < contents.size(); ++index) {
145 const ContentInfo* cinfo = &contents[index];
146 if (cinfo->rejected) {
147 continue;
148 }
149
150 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
151 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
152 if (!tinfo) {
153 // Something is not right.
154 LOG(LS_ERROR) << kInvalidSdp;
155 return false;
156 }
157 if (tinfo->description.ice_ufrag.empty() ||
158 tinfo->description.ice_pwd.empty()) {
159 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
160 return false;
161 }
162 }
163 return true;
164}
165
wu@webrtc.org91053e72013-08-10 07:18:04 +0000166// Forces |sdesc->crypto_required| to the appropriate state based on the
167// current security policy, to ensure a failure occurs if there is an error
168// in crypto negotiation.
169// Called when processing the local session description.
170static void UpdateSessionDescriptionSecurePolicy(
171 cricket::SecureMediaPolicy secure_policy,
172 SessionDescription* sdesc) {
173 if (!sdesc) {
174 return;
175 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176
wu@webrtc.org91053e72013-08-10 07:18:04 +0000177 // Updating the |crypto_required_| in MediaContentDescription to the
178 // appropriate state based on the current security policy.
179 for (cricket::ContentInfos::iterator iter = sdesc->contents().begin();
180 iter != sdesc->contents().end(); ++iter) {
181 if (cricket::IsMediaContent(&*iter)) {
182 MediaContentDescription* mdesc =
183 static_cast<MediaContentDescription*> (iter->description);
184 if (mdesc) {
185 mdesc->set_crypto_required(secure_policy == cricket::SEC_REQUIRED);
186 }
187 }
188 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189}
190
191static bool GetAudioSsrcByTrackId(
192 const SessionDescription* session_description,
193 const std::string& track_id, uint32 *ssrc) {
194 const cricket::ContentInfo* audio_info =
195 cricket::GetFirstAudioContent(session_description);
196 if (!audio_info) {
197 LOG(LS_ERROR) << "Audio not used in this call";
198 return false;
199 }
200
201 const cricket::MediaContentDescription* audio_content =
202 static_cast<const cricket::MediaContentDescription*>(
203 audio_info->description);
204 cricket::StreamParams stream;
205 if (!cricket::GetStreamByIds(audio_content->streams(), "", track_id,
206 &stream)) {
207 return false;
208 }
209 *ssrc = stream.first_ssrc();
210 return true;
211}
212
213static bool GetTrackIdBySsrc(const SessionDescription* session_description,
214 uint32 ssrc, std::string* track_id) {
215 ASSERT(track_id != NULL);
216
217 cricket::StreamParams stream_out;
218 const cricket::ContentInfo* audio_info =
219 cricket::GetFirstAudioContent(session_description);
220 if (!audio_info) {
221 return false;
222 }
223 const cricket::MediaContentDescription* audio_content =
224 static_cast<const cricket::MediaContentDescription*>(
225 audio_info->description);
226
227 if (cricket::GetStreamBySsrc(audio_content->streams(), ssrc, &stream_out)) {
228 *track_id = stream_out.id;
229 return true;
230 }
231
232 const cricket::ContentInfo* video_info =
233 cricket::GetFirstVideoContent(session_description);
234 if (!video_info) {
235 return false;
236 }
237 const cricket::MediaContentDescription* video_content =
238 static_cast<const cricket::MediaContentDescription*>(
239 video_info->description);
240
241 if (cricket::GetStreamBySsrc(video_content->streams(), ssrc, &stream_out)) {
242 *track_id = stream_out.id;
243 return true;
244 }
245 return false;
246}
247
248static bool BadSdp(const std::string& desc, std::string* err_desc) {
249 if (err_desc) {
250 *err_desc = desc;
251 }
252 LOG(LS_ERROR) << desc;
253 return false;
254}
255
256static bool BadLocalSdp(const std::string& desc, std::string* err_desc) {
257 std::string set_local_sdp_failed = kSetLocalSdpFailed;
258 set_local_sdp_failed.append(desc);
259 return BadSdp(set_local_sdp_failed, err_desc);
260}
261
262static bool BadRemoteSdp(const std::string& desc, std::string* err_desc) {
263 std::string set_remote_sdp_failed = kSetRemoteSdpFailed;
264 set_remote_sdp_failed.append(desc);
265 return BadSdp(set_remote_sdp_failed, err_desc);
266}
267
268static bool BadSdp(cricket::ContentSource source,
269 const std::string& desc, std::string* err_desc) {
270 if (source == cricket::CS_LOCAL) {
271 return BadLocalSdp(desc, err_desc);
272 } else {
273 return BadRemoteSdp(desc, err_desc);
274 }
275}
276
277static std::string SessionErrorMsg(cricket::BaseSession::Error error) {
278 std::ostringstream desc;
279 desc << kSessionError << error;
280 return desc.str();
281}
282
283#define GET_STRING_OF_STATE(state) \
284 case cricket::BaseSession::state: \
285 result = #state; \
286 break;
287
288static std::string GetStateString(cricket::BaseSession::State state) {
289 std::string result;
290 switch (state) {
291 GET_STRING_OF_STATE(STATE_INIT)
292 GET_STRING_OF_STATE(STATE_SENTINITIATE)
293 GET_STRING_OF_STATE(STATE_RECEIVEDINITIATE)
294 GET_STRING_OF_STATE(STATE_SENTPRACCEPT)
295 GET_STRING_OF_STATE(STATE_SENTACCEPT)
296 GET_STRING_OF_STATE(STATE_RECEIVEDPRACCEPT)
297 GET_STRING_OF_STATE(STATE_RECEIVEDACCEPT)
298 GET_STRING_OF_STATE(STATE_SENTMODIFY)
299 GET_STRING_OF_STATE(STATE_RECEIVEDMODIFY)
300 GET_STRING_OF_STATE(STATE_SENTREJECT)
301 GET_STRING_OF_STATE(STATE_RECEIVEDREJECT)
302 GET_STRING_OF_STATE(STATE_SENTREDIRECT)
303 GET_STRING_OF_STATE(STATE_SENTTERMINATE)
304 GET_STRING_OF_STATE(STATE_RECEIVEDTERMINATE)
305 GET_STRING_OF_STATE(STATE_INPROGRESS)
306 GET_STRING_OF_STATE(STATE_DEINIT)
307 default:
308 ASSERT(false);
309 break;
310 }
311 return result;
312}
313
314#define GET_STRING_OF_ERROR(err) \
315 case cricket::BaseSession::err: \
316 result = #err; \
317 break;
318
319static std::string GetErrorString(cricket::BaseSession::Error err) {
320 std::string result;
321 switch (err) {
322 GET_STRING_OF_ERROR(ERROR_NONE)
323 GET_STRING_OF_ERROR(ERROR_TIME)
324 GET_STRING_OF_ERROR(ERROR_RESPONSE)
325 GET_STRING_OF_ERROR(ERROR_NETWORK)
326 GET_STRING_OF_ERROR(ERROR_CONTENT)
327 GET_STRING_OF_ERROR(ERROR_TRANSPORT)
328 default:
329 ASSERT(false);
330 break;
331 }
332 return result;
333}
334
335static bool SetSessionStateFailed(cricket::ContentSource source,
336 cricket::BaseSession::Error err,
337 std::string* err_desc) {
338 std::string set_state_err = kUpdateStateFailed;
339 set_state_err.append(GetErrorString(err));
340 return BadSdp(source, set_state_err, err_desc);
341}
342
343// Help class used to remember if a a remote peer has requested ice restart by
344// by sending a description with new ice ufrag and password.
345class IceRestartAnswerLatch {
346 public:
347 IceRestartAnswerLatch() : ice_restart_(false) { }
348
wu@webrtc.org91053e72013-08-10 07:18:04 +0000349 // Returns true if CheckForRemoteIceRestart has been called with a new session
350 // description where ice password and ufrag has changed since last time
351 // Reset() was called.
352 bool Get() const {
353 return ice_restart_;
354 }
355
356 void Reset() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357 if (ice_restart_) {
358 ice_restart_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000360 }
361
362 void CheckForRemoteIceRestart(
363 const SessionDescriptionInterface* old_desc,
364 const SessionDescriptionInterface* new_desc) {
365 if (!old_desc || new_desc->type() != SessionDescriptionInterface::kOffer) {
366 return;
367 }
368 const SessionDescription* new_sd = new_desc->description();
369 const SessionDescription* old_sd = old_desc->description();
370 const ContentInfos& contents = new_sd->contents();
371 for (size_t index = 0; index < contents.size(); ++index) {
372 const ContentInfo* cinfo = &contents[index];
373 if (cinfo->rejected) {
374 continue;
375 }
376 // If the content isn't rejected, check if ufrag and password has
377 // changed.
378 const cricket::TransportDescription* new_transport_desc =
379 new_sd->GetTransportDescriptionByName(cinfo->name);
380 const cricket::TransportDescription* old_transport_desc =
381 old_sd->GetTransportDescriptionByName(cinfo->name);
382 if (!new_transport_desc || !old_transport_desc) {
383 // No transport description exist. This is not an ice restart.
384 continue;
385 }
386 if (new_transport_desc->ice_pwd != old_transport_desc->ice_pwd &&
387 new_transport_desc->ice_ufrag != old_transport_desc->ice_ufrag) {
388 LOG(LS_INFO) << "Remote peer request ice restart.";
389 ice_restart_ = true;
390 break;
391 }
392 }
393 }
394
395 private:
396 bool ice_restart_;
397};
398
wu@webrtc.org91053e72013-08-10 07:18:04 +0000399WebRtcSession::WebRtcSession(
400 cricket::ChannelManager* channel_manager,
401 talk_base::Thread* signaling_thread,
402 talk_base::Thread* worker_thread,
403 cricket::PortAllocator* port_allocator,
404 MediaStreamSignaling* mediastream_signaling)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405 : cricket::BaseSession(signaling_thread, worker_thread, port_allocator,
406 talk_base::ToString(talk_base::CreateRandomId64() &
407 LLONG_MAX),
408 cricket::NS_JINGLE_RTP, false),
409 // RFC 3264: The numeric value of the session id and version in the
410 // o line MUST be representable with a "64 bit signed integer".
411 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
412 channel_manager_(channel_manager),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413 mediastream_signaling_(mediastream_signaling),
414 ice_observer_(NULL),
415 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000417 dtls_enabled_(false),
wu@webrtc.orgde305012013-10-31 15:40:38 +0000418 dscp_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419 data_channel_type_(cricket::DCT_NONE),
420 ice_restart_latch_(new IceRestartAnswerLatch) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000421}
422
423WebRtcSession::~WebRtcSession() {
424 if (voice_channel_.get()) {
425 SignalVoiceChannelDestroyed();
426 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
427 }
428 if (video_channel_.get()) {
429 SignalVideoChannelDestroyed();
430 channel_manager_->DestroyVideoChannel(video_channel_.release());
431 }
432 if (data_channel_.get()) {
433 SignalDataChannelDestroyed();
434 channel_manager_->DestroyDataChannel(data_channel_.release());
435 }
436 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
437 delete saved_candidates_[i];
438 }
439 delete identity();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000440}
441
wu@webrtc.org91053e72013-08-10 07:18:04 +0000442bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000443 const PeerConnectionFactoryInterface::Options& options,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000444 const MediaConstraintsInterface* constraints,
445 DTLSIdentityServiceInterface* dtls_identity_service) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000446 // TODO(perkj): Take |constraints| into consideration. Return false if not all
447 // mandatory constraints can be fulfilled. Note that |constraints|
448 // can be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449 bool value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000450
451 // Enable DTLS by default if |dtls_identity_service| is valid.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000452 dtls_enabled_ = (dtls_identity_service != NULL);
453 // |constraints| can override the default |dtls_enabled_| value.
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000454 if (FindConstraint(
455 constraints,
456 MediaConstraintsInterface::kEnableDtlsSrtp,
457 &value, NULL)) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000458 dtls_enabled_ = value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000459 }
460
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000462 // It takes precendence over the disable_sctp_data_channels
463 // PeerConnectionFactoryInterface::Options.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464 if (FindConstraint(
465 constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
466 &value, NULL) && value) {
467 LOG(LS_INFO) << "Allowing RTP data engine.";
468 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000469 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000470 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000471 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000472 LOG(LS_INFO) << "Allowing SCTP data engine.";
473 data_channel_type_ = cricket::DCT_SCTP;
474 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000475 }
476 if (data_channel_type_ != cricket::DCT_NONE) {
477 mediastream_signaling_->SetDataChannelFactory(this);
478 }
479
wu@webrtc.orgde305012013-10-31 15:40:38 +0000480 // Find DSCP constraint.
481 if (FindConstraint(
482 constraints,
483 MediaConstraintsInterface::kEnableDscp,
484 &value, NULL)) {
485 dscp_enabled_ = value;
486 }
487
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000488 const cricket::VideoCodec default_codec(
489 JsepSessionDescription::kDefaultVideoCodecId,
490 JsepSessionDescription::kDefaultVideoCodecName,
491 JsepSessionDescription::kMaxVideoCodecWidth,
492 JsepSessionDescription::kMaxVideoCodecHeight,
493 JsepSessionDescription::kDefaultVideoCodecFramerate,
494 JsepSessionDescription::kDefaultVideoCodecPreference);
495 channel_manager_->SetDefaultVideoEncoderConfig(
496 cricket::VideoEncoderConfig(default_codec));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000497
498 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
499 signaling_thread(),
500 channel_manager_,
501 mediastream_signaling_,
502 dtls_identity_service,
503 this,
504 id(),
505 data_channel_type_,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000506 dtls_enabled_));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000507
508 webrtc_session_desc_factory_->SignalIdentityReady.connect(
509 this, &WebRtcSession::OnIdentityReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000510
wu@webrtc.org97077a32013-10-25 21:18:33 +0000511 if (options.disable_encryption) {
wu@webrtc.org364f2042013-11-20 21:49:41 +0000512 webrtc_session_desc_factory_->SetSecure(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000513 }
514
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515 return true;
516}
517
518void WebRtcSession::Terminate() {
519 SetState(STATE_RECEIVEDTERMINATE);
520 RemoveUnusedChannelsAndTransports(NULL);
521 ASSERT(voice_channel_.get() == NULL);
522 ASSERT(video_channel_.get() == NULL);
523 ASSERT(data_channel_.get() == NULL);
524}
525
526bool WebRtcSession::StartCandidatesAllocation() {
527 // SpeculativelyConnectTransportChannels, will call ConnectChannels method
528 // from TransportProxy to start gathering ice candidates.
529 SpeculativelyConnectAllTransportChannels();
530 if (!saved_candidates_.empty()) {
531 // If there are saved candidates which arrived before local description is
532 // set, copy those to remote description.
533 CopySavedCandidates(remote_desc_.get());
534 }
535 // Push remote candidates present in remote description to transport channels.
536 UseCandidatesInSessionDescription(remote_desc_.get());
537 return true;
538}
539
wu@webrtc.org364f2042013-11-20 21:49:41 +0000540void WebRtcSession::SetSecurePolicy(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541 cricket::SecureMediaPolicy secure_policy) {
wu@webrtc.org364f2042013-11-20 21:49:41 +0000542 webrtc_session_desc_factory_->SetSecure(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543}
544
wu@webrtc.org364f2042013-11-20 21:49:41 +0000545cricket::SecureMediaPolicy WebRtcSession::SecurePolicy() const {
546 return webrtc_session_desc_factory_->Secure();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547}
548
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000549bool WebRtcSession::GetSslRole(talk_base::SSLRole* role) {
550 if (local_description() == NULL || remote_description() == NULL) {
551 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
552 << "SSL Role of the session.";
553 return false;
554 }
555
556 // TODO(mallinath) - Return role of each transport, as role may differ from
557 // one another.
558 // In current implementaion we just return the role of first transport in the
559 // transport map.
560 for (cricket::TransportMap::const_iterator iter = transport_proxies().begin();
561 iter != transport_proxies().end(); ++iter) {
562 if (iter->second->impl()) {
563 return iter->second->impl()->GetSslRole(role);
564 }
565 }
566 return false;
567}
568
wu@webrtc.org91053e72013-08-10 07:18:04 +0000569void WebRtcSession::CreateOffer(CreateSessionDescriptionObserver* observer,
570 const MediaConstraintsInterface* constraints) {
571 webrtc_session_desc_factory_->CreateOffer(observer, constraints);
572}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000573
wu@webrtc.org91053e72013-08-10 07:18:04 +0000574void WebRtcSession::CreateAnswer(CreateSessionDescriptionObserver* observer,
575 const MediaConstraintsInterface* constraints) {
576 webrtc_session_desc_factory_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000577}
578
579bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
580 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000581 // Takes the ownership of |desc| regardless of the result.
582 talk_base::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
583
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000584 // Validate SDP.
585 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
586 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587 }
588
589 // Update the initiator flag if this session is the initiator.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000590 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 if (state() == STATE_INIT && action == kOffer) {
592 set_initiator(true);
593 }
594
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000595 cricket::SecureMediaPolicy secure_policy =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000596 webrtc_session_desc_factory_->Secure();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000597 // Update the MediaContentDescription crypto settings as per the policy set.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000598 UpdateSessionDescriptionSecurePolicy(secure_policy, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599
600 set_local_description(desc->description()->Copy());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000601 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602
603 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000604 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605 // TODO(mallinath) - Handle CreateChannel failure, as new local description
606 // is applied. Restore back to old description.
607 return BadLocalSdp(kCreateChannelFailed, err_desc);
608 }
609
610 // Remove channel and transport proxies, if MediaContentDescription is
611 // rejected.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000612 RemoveUnusedChannelsAndTransports(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613
614 if (!UpdateSessionState(action, cricket::CS_LOCAL,
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000615 local_desc_->description(), err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616 return false;
617 }
618 // Kick starting the ice candidates allocation.
619 StartCandidatesAllocation();
620
621 // Update state and SSRC of local MediaStreams and DataChannels based on the
622 // local session description.
623 mediastream_signaling_->OnLocalDescriptionChanged(local_desc_.get());
624
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000625 talk_base::SSLRole role;
626 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
627 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
628 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 if (error() != cricket::BaseSession::ERROR_NONE) {
630 return BadLocalSdp(SessionErrorMsg(error()), err_desc);
631 }
632 return true;
633}
634
635bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
636 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000637 // Takes the ownership of |desc| regardless of the result.
638 talk_base::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
639
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000640 // Validate SDP.
641 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
642 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643 }
644
645 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000646 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647 if (action == kOffer && !CreateChannels(desc->description())) {
648 // TODO(mallinath) - Handle CreateChannel failure, as new local description
649 // is applied. Restore back to old description.
650 return BadRemoteSdp(kCreateChannelFailed, err_desc);
651 }
652
653 // Remove channel and transport proxies, if MediaContentDescription is
654 // rejected.
655 RemoveUnusedChannelsAndTransports(desc->description());
656
657 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
658 // is called.
659 set_remote_description(desc->description()->Copy());
660 if (!UpdateSessionState(action, cricket::CS_REMOTE,
661 desc->description(), err_desc)) {
662 return false;
663 }
664
665 // Update remote MediaStreams.
666 mediastream_signaling_->OnRemoteDescriptionChanged(desc);
667 if (local_description() && !UseCandidatesInSessionDescription(desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000668 return BadRemoteSdp(kInvalidCandidates, err_desc);
669 }
670
671 // Copy all saved candidates.
672 CopySavedCandidates(desc);
673 // We retain all received candidates.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000674 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
675 remote_desc_.get(), desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 // Check if this new SessionDescription contains new ice ufrag and password
677 // that indicates the remote peer requests ice restart.
678 ice_restart_latch_->CheckForRemoteIceRestart(remote_desc_.get(),
679 desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000680 remote_desc_.reset(desc_temp.release());
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000681
682 talk_base::SSLRole role;
683 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
684 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
685 }
686
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687 if (error() != cricket::BaseSession::ERROR_NONE) {
688 return BadRemoteSdp(SessionErrorMsg(error()), err_desc);
689 }
690 return true;
691}
692
693bool WebRtcSession::UpdateSessionState(
694 Action action, cricket::ContentSource source,
695 const cricket::SessionDescription* desc,
696 std::string* err_desc) {
697 // If there's already a pending error then no state transition should happen.
698 // But all call-sites should be verifying this before calling us!
699 ASSERT(error() == cricket::BaseSession::ERROR_NONE);
700 if (action == kOffer) {
701 if (!PushdownTransportDescription(source, cricket::CA_OFFER)) {
702 return BadSdp(source, kPushDownOfferTDFailed, err_desc);
703 }
704 SetState(source == cricket::CS_LOCAL ?
705 STATE_SENTINITIATE : STATE_RECEIVEDINITIATE);
706 if (error() != cricket::BaseSession::ERROR_NONE) {
707 return SetSessionStateFailed(source, error(), err_desc);
708 }
709 } else if (action == kPrAnswer) {
710 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER)) {
711 return BadSdp(source, kPushDownPranswerTDFailed, err_desc);
712 }
713 EnableChannels();
714 SetState(source == cricket::CS_LOCAL ?
715 STATE_SENTPRACCEPT : STATE_RECEIVEDPRACCEPT);
716 if (error() != cricket::BaseSession::ERROR_NONE) {
717 return SetSessionStateFailed(source, error(), err_desc);
718 }
719 } else if (action == kAnswer) {
720 if (!PushdownTransportDescription(source, cricket::CA_ANSWER)) {
721 return BadSdp(source, kPushDownAnswerTDFailed, err_desc);
722 }
723 MaybeEnableMuxingSupport();
724 EnableChannels();
725 SetState(source == cricket::CS_LOCAL ?
726 STATE_SENTACCEPT : STATE_RECEIVEDACCEPT);
727 if (error() != cricket::BaseSession::ERROR_NONE) {
728 return SetSessionStateFailed(source, error(), err_desc);
729 }
730 }
731 return true;
732}
733
734WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
735 if (type == SessionDescriptionInterface::kOffer) {
736 return WebRtcSession::kOffer;
737 } else if (type == SessionDescriptionInterface::kPrAnswer) {
738 return WebRtcSession::kPrAnswer;
739 } else if (type == SessionDescriptionInterface::kAnswer) {
740 return WebRtcSession::kAnswer;
741 }
742 ASSERT(false && "unknown action type");
743 return WebRtcSession::kOffer;
744}
745
746bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
747 if (state() == STATE_INIT) {
748 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
749 << "without any offer (local or remote) "
750 << "session description.";
751 return false;
752 }
753
754 if (!candidate) {
755 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL";
756 return false;
757 }
758
759 if (!local_description() || !remote_description()) {
760 LOG(LS_INFO) << "ProcessIceMessage: Remote description not set, "
761 << "save the candidate for later use.";
762 saved_candidates_.push_back(
763 new JsepIceCandidate(candidate->sdp_mid(), candidate->sdp_mline_index(),
764 candidate->candidate()));
765 return true;
766 }
767
768 // Add this candidate to the remote session description.
769 if (!remote_desc_->AddCandidate(candidate)) {
770 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used";
771 return false;
772 }
773
774 return UseCandidatesInSessionDescription(remote_desc_.get());
775}
776
777bool WebRtcSession::GetTrackIdBySsrc(uint32 ssrc, std::string* id) {
778 if (GetLocalTrackId(ssrc, id)) {
779 if (GetRemoteTrackId(ssrc, id)) {
780 LOG(LS_WARNING) << "SSRC " << ssrc
781 << " exists in both local and remote descriptions";
782 return true; // We return the remote track id.
783 }
784 return true;
785 } else {
786 return GetRemoteTrackId(ssrc, id);
787 }
788}
789
790bool WebRtcSession::GetLocalTrackId(uint32 ssrc, std::string* track_id) {
791 if (!BaseSession::local_description())
792 return false;
793 return webrtc::GetTrackIdBySsrc(
794 BaseSession::local_description(), ssrc, track_id);
795}
796
797bool WebRtcSession::GetRemoteTrackId(uint32 ssrc, std::string* track_id) {
798 if (!BaseSession::remote_description())
799 return false;
800 return webrtc::GetTrackIdBySsrc(
801 BaseSession::remote_description(), ssrc, track_id);
802}
803
804std::string WebRtcSession::BadStateErrMsg(
805 const std::string& type, State state) {
806 std::ostringstream desc;
807 desc << "Called with type in wrong state, "
808 << "type: " << type << " state: " << GetStateString(state);
809 return desc.str();
810}
811
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000812void WebRtcSession::SetAudioPlayout(uint32 ssrc, bool enable,
813 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 ASSERT(signaling_thread()->IsCurrent());
815 if (!voice_channel_) {
816 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
817 return;
818 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000819 if (!voice_channel_->SetRemoteRenderer(ssrc, renderer)) {
820 // SetRenderer() can fail if the ssrc does not match any playout channel.
821 LOG(LS_ERROR) << "SetAudioPlayout: ssrc is incorrect: " << ssrc;
822 return;
823 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824 if (!voice_channel_->SetOutputScaling(ssrc, enable ? 1 : 0, enable ? 1 : 0)) {
825 // Allow that SetOutputScaling fail if |enable| is false but assert
826 // otherwise. This in the normal case when the underlying media channel has
827 // already been deleted.
828 ASSERT(enable == false);
829 }
830}
831
832void WebRtcSession::SetAudioSend(uint32 ssrc, bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000833 const cricket::AudioOptions& options,
834 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835 ASSERT(signaling_thread()->IsCurrent());
836 if (!voice_channel_) {
837 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
838 return;
839 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000840 if (!voice_channel_->SetLocalRenderer(ssrc, renderer)) {
841 // SetRenderer() can fail if the ssrc does not match any send channel.
842 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
843 return;
844 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000845 if (!voice_channel_->MuteStream(ssrc, !enable)) {
846 // Allow that MuteStream fail if |enable| is false but assert otherwise.
847 // This in the normal case when the underlying media channel has already
848 // been deleted.
849 ASSERT(enable == false);
850 return;
851 }
852 if (enable)
853 voice_channel_->SetChannelOptions(options);
854}
855
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000856bool WebRtcSession::SetCaptureDevice(uint32 ssrc,
857 cricket::VideoCapturer* camera) {
858 ASSERT(signaling_thread()->IsCurrent());
859
860 if (!video_channel_.get()) {
861 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
862 // support video.
863 LOG(LS_WARNING) << "Video not used in this call.";
864 return false;
865 }
866 if (!video_channel_->SetCapturer(ssrc, camera)) {
867 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
868 // This in the normal case when the underlying media channel has already
869 // been deleted.
870 ASSERT(camera == NULL);
871 return false;
872 }
873 return true;
874}
875
876void WebRtcSession::SetVideoPlayout(uint32 ssrc,
877 bool enable,
878 cricket::VideoRenderer* renderer) {
879 ASSERT(signaling_thread()->IsCurrent());
880 if (!video_channel_) {
881 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
882 return;
883 }
884 if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) {
885 // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise.
886 // This in the normal case when the underlying media channel has already
887 // been deleted.
888 ASSERT(renderer == NULL);
889 }
890}
891
892void WebRtcSession::SetVideoSend(uint32 ssrc, bool enable,
893 const cricket::VideoOptions* options) {
894 ASSERT(signaling_thread()->IsCurrent());
895 if (!video_channel_) {
896 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
897 return;
898 }
899 if (!video_channel_->MuteStream(ssrc, !enable)) {
900 // Allow that MuteStream fail if |enable| is false but assert otherwise.
901 // This in the normal case when the underlying media channel has already
902 // been deleted.
903 ASSERT(enable == false);
904 return;
905 }
906 if (enable && options)
907 video_channel_->SetChannelOptions(*options);
908}
909
910bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
911 ASSERT(signaling_thread()->IsCurrent());
912 if (!voice_channel_) {
913 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
914 return false;
915 }
916 uint32 send_ssrc = 0;
917 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
918 // exists.
919 if (!GetAudioSsrcByTrackId(BaseSession::local_description(), track_id,
920 &send_ssrc)) {
921 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
922 return false;
923 }
924 return voice_channel_->CanInsertDtmf();
925}
926
927bool WebRtcSession::InsertDtmf(const std::string& track_id,
928 int code, int duration) {
929 ASSERT(signaling_thread()->IsCurrent());
930 if (!voice_channel_) {
931 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
932 return false;
933 }
934 uint32 send_ssrc = 0;
935 if (!VERIFY(GetAudioSsrcByTrackId(BaseSession::local_description(),
936 track_id, &send_ssrc))) {
937 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
938 return false;
939 }
940 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration,
941 cricket::DF_SEND)) {
942 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
943 return false;
944 }
945 return true;
946}
947
948sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
949 return &SignalVoiceChannelDestroyed;
950}
951
wu@webrtc.org78187522013-10-07 23:32:02 +0000952bool WebRtcSession::SendData(const cricket::SendDataParams& params,
953 const talk_base::Buffer& payload,
954 cricket::SendDataResult* result) {
955 if (!data_channel_.get()) {
956 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
957 return false;
958 }
959 return data_channel_->SendData(params, payload, result);
960}
961
962bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
963 if (!data_channel_.get()) {
964 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
965 return false;
966 }
wu@webrtc.org78187522013-10-07 23:32:02 +0000967 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
968 &DataChannel::OnChannelReady);
969 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
970 &DataChannel::OnDataReceived);
wu@webrtc.org78187522013-10-07 23:32:02 +0000971 return true;
972}
973
974void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000975 if (!data_channel_.get()) {
976 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
977 return;
978 }
wu@webrtc.org78187522013-10-07 23:32:02 +0000979 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
980 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
981}
982
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000983void WebRtcSession::AddSctpDataStream(uint32 sid) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000984 if (!data_channel_.get()) {
985 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
986 return;
987 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000988 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
989 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000990}
991
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000992void WebRtcSession::RemoveSctpDataStream(uint32 sid) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000993 if (!data_channel_.get()) {
994 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
995 << "NULL.";
996 return;
997 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000998 data_channel_->RemoveRecvStream(sid);
999 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001000}
1001
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001002bool WebRtcSession::ReadyToSendData() const {
1003 return data_channel_.get() && data_channel_->ready_to_send_data();
1004}
1005
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006talk_base::scoped_refptr<DataChannel> WebRtcSession::CreateDataChannel(
wu@webrtc.org78187522013-10-07 23:32:02 +00001007 const std::string& label,
mallinath@webrtc.org0f3356e2014-01-11 01:26:23 +00001008 const InternalDataChannelInit* config) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001009 if (state() == STATE_RECEIVEDTERMINATE) {
1010 return NULL;
1011 }
1012 if (data_channel_type_ == cricket::DCT_NONE) {
1013 LOG(LS_ERROR) << "CreateDataChannel: Data is not supported in this call.";
1014 return NULL;
1015 }
mallinath@webrtc.org0f3356e2014-01-11 01:26:23 +00001016 InternalDataChannelInit new_config =
1017 config ? (*config) : InternalDataChannelInit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001018 if (data_channel_type_ == cricket::DCT_SCTP) {
1019 if (new_config.id < 0) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001020 talk_base::SSLRole role;
1021 if (GetSslRole(&role) &&
1022 !mediastream_signaling_->AllocateSctpSid(role, &new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001023 LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel.";
1024 return NULL;
1025 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001026 } else if (!mediastream_signaling_->IsSctpSidAvailable(new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001027 LOG(LS_ERROR) << "Failed to create a SCTP data channel "
1028 << "because the id is already in use or out of range.";
1029 return NULL;
1030 }
1031 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001032
mallinath@webrtc.org0f3356e2014-01-11 01:26:23 +00001033 talk_base::scoped_refptr<DataChannel> channel(DataChannel::Create(
1034 this, data_channel_type_, label, new_config));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001035 if (channel && !mediastream_signaling_->AddDataChannel(channel))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001036 return NULL;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001037
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001038 return channel;
1039}
1040
1041cricket::DataChannelType WebRtcSession::data_channel_type() const {
1042 return data_channel_type_;
1043}
1044
wu@webrtc.org91053e72013-08-10 07:18:04 +00001045bool WebRtcSession::IceRestartPending() const {
1046 return ice_restart_latch_->Get();
1047}
1048
1049void WebRtcSession::ResetIceRestartLatch() {
1050 ice_restart_latch_->Reset();
1051}
1052
1053void WebRtcSession::OnIdentityReady(talk_base::SSLIdentity* identity) {
1054 SetIdentity(identity);
1055}
1056
1057bool WebRtcSession::waiting_for_identity() const {
1058 return webrtc_session_desc_factory_->waiting_for_identity();
1059}
1060
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001061void WebRtcSession::SetIceConnectionState(
1062 PeerConnectionInterface::IceConnectionState state) {
1063 if (ice_connection_state_ == state) {
1064 return;
1065 }
1066
1067 // ASSERT that the requested transition is allowed. Note that
1068 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1069 // within PeerConnection). This switch statement should compile away when
1070 // ASSERTs are disabled.
1071 switch (ice_connection_state_) {
1072 case PeerConnectionInterface::kIceConnectionNew:
1073 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1074 break;
1075 case PeerConnectionInterface::kIceConnectionChecking:
1076 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1077 state == PeerConnectionInterface::kIceConnectionConnected);
1078 break;
1079 case PeerConnectionInterface::kIceConnectionConnected:
1080 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1081 state == PeerConnectionInterface::kIceConnectionChecking ||
1082 state == PeerConnectionInterface::kIceConnectionCompleted);
1083 break;
1084 case PeerConnectionInterface::kIceConnectionCompleted:
1085 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1086 state == PeerConnectionInterface::kIceConnectionDisconnected);
1087 break;
1088 case PeerConnectionInterface::kIceConnectionFailed:
1089 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1090 break;
1091 case PeerConnectionInterface::kIceConnectionDisconnected:
1092 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1093 state == PeerConnectionInterface::kIceConnectionConnected ||
1094 state == PeerConnectionInterface::kIceConnectionCompleted ||
1095 state == PeerConnectionInterface::kIceConnectionFailed);
1096 break;
1097 case PeerConnectionInterface::kIceConnectionClosed:
1098 ASSERT(false);
1099 break;
1100 default:
1101 ASSERT(false);
1102 break;
1103 }
1104
1105 ice_connection_state_ = state;
1106 if (ice_observer_) {
1107 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1108 }
1109}
1110
1111void WebRtcSession::OnTransportRequestSignaling(
1112 cricket::Transport* transport) {
1113 ASSERT(signaling_thread()->IsCurrent());
1114 transport->OnSignalingReady();
1115 if (ice_observer_) {
1116 ice_observer_->OnIceGatheringChange(
1117 PeerConnectionInterface::kIceGatheringGathering);
1118 }
1119}
1120
1121void WebRtcSession::OnTransportConnecting(cricket::Transport* transport) {
1122 ASSERT(signaling_thread()->IsCurrent());
1123 // start monitoring for the write state of the transport.
1124 OnTransportWritable(transport);
1125}
1126
1127void WebRtcSession::OnTransportWritable(cricket::Transport* transport) {
1128 ASSERT(signaling_thread()->IsCurrent());
1129 // TODO(bemasc): Expose more API from Transport to detect when
1130 // candidate selection starts or stops, due to success or failure.
1131 if (transport->all_channels_writable()) {
1132 if (ice_connection_state_ ==
1133 PeerConnectionInterface::kIceConnectionChecking ||
1134 ice_connection_state_ ==
1135 PeerConnectionInterface::kIceConnectionDisconnected) {
1136 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1137 }
1138 } else if (transport->HasChannels()) {
1139 // If the current state is Connected or Completed, then there were writable
1140 // channels but now there are not, so the next state must be Disconnected.
1141 if (ice_connection_state_ ==
1142 PeerConnectionInterface::kIceConnectionConnected ||
1143 ice_connection_state_ ==
1144 PeerConnectionInterface::kIceConnectionCompleted) {
1145 SetIceConnectionState(
1146 PeerConnectionInterface::kIceConnectionDisconnected);
1147 }
1148 }
1149}
1150
1151void WebRtcSession::OnTransportProxyCandidatesReady(
1152 cricket::TransportProxy* proxy, const cricket::Candidates& candidates) {
1153 ASSERT(signaling_thread()->IsCurrent());
1154 ProcessNewLocalCandidate(proxy->content_name(), candidates);
1155}
1156
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001157void WebRtcSession::OnCandidatesAllocationDone() {
1158 ASSERT(signaling_thread()->IsCurrent());
1159 if (ice_observer_) {
1160 ice_observer_->OnIceGatheringChange(
1161 PeerConnectionInterface::kIceGatheringComplete);
1162 ice_observer_->OnIceComplete();
1163 }
1164}
1165
1166// Enabling voice and video channel.
1167void WebRtcSession::EnableChannels() {
1168 if (voice_channel_ && !voice_channel_->enabled())
1169 voice_channel_->Enable(true);
1170
1171 if (video_channel_ && !video_channel_->enabled())
1172 video_channel_->Enable(true);
1173
1174 if (data_channel_.get() && !data_channel_->enabled())
1175 data_channel_->Enable(true);
1176}
1177
1178void WebRtcSession::ProcessNewLocalCandidate(
1179 const std::string& content_name,
1180 const cricket::Candidates& candidates) {
1181 int sdp_mline_index;
1182 if (!GetLocalCandidateMediaIndex(content_name, &sdp_mline_index)) {
1183 LOG(LS_ERROR) << "ProcessNewLocalCandidate: content name "
1184 << content_name << " not found";
1185 return;
1186 }
1187
1188 for (cricket::Candidates::const_iterator citer = candidates.begin();
1189 citer != candidates.end(); ++citer) {
1190 // Use content_name as the candidate media id.
1191 JsepIceCandidate candidate(content_name, sdp_mline_index, *citer);
1192 if (ice_observer_) {
1193 ice_observer_->OnIceCandidate(&candidate);
1194 }
1195 if (local_desc_) {
1196 local_desc_->AddCandidate(&candidate);
1197 }
1198 }
1199}
1200
1201// Returns the media index for a local ice candidate given the content name.
1202bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1203 int* sdp_mline_index) {
1204 if (!BaseSession::local_description() || !sdp_mline_index)
1205 return false;
1206
1207 bool content_found = false;
1208 const ContentInfos& contents = BaseSession::local_description()->contents();
1209 for (size_t index = 0; index < contents.size(); ++index) {
1210 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001211 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001212 content_found = true;
1213 break;
1214 }
1215 }
1216 return content_found;
1217}
1218
1219bool WebRtcSession::UseCandidatesInSessionDescription(
1220 const SessionDescriptionInterface* remote_desc) {
1221 if (!remote_desc)
1222 return true;
1223 bool ret = true;
1224 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1225 const IceCandidateCollection* candidates = remote_desc->candidates(m);
1226 for (size_t n = 0; n < candidates->count(); ++n) {
1227 ret = UseCandidate(candidates->at(n));
1228 if (!ret)
1229 break;
1230 }
1231 }
1232 return ret;
1233}
1234
1235bool WebRtcSession::UseCandidate(
1236 const IceCandidateInterface* candidate) {
1237
1238 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
1239 size_t remote_content_size =
1240 BaseSession::remote_description()->contents().size();
1241 if (mediacontent_index >= remote_content_size) {
1242 LOG(LS_ERROR)
1243 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1244 return false;
1245 }
1246
1247 cricket::ContentInfo content =
1248 BaseSession::remote_description()->contents()[mediacontent_index];
1249 std::vector<cricket::Candidate> candidates;
1250 candidates.push_back(candidate->candidate());
1251 // Invoking BaseSession method to handle remote candidates.
1252 std::string error;
1253 if (OnRemoteCandidates(content.name, candidates, &error)) {
1254 // Candidates successfully submitted for checking.
1255 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1256 ice_connection_state_ ==
1257 PeerConnectionInterface::kIceConnectionDisconnected) {
1258 // If state is New, then the session has just gotten its first remote ICE
1259 // candidates, so go to Checking.
1260 // If state is Disconnected, the session is re-using old candidates or
1261 // receiving additional ones, so go to Checking.
1262 // If state is Connected, stay Connected.
1263 // TODO(bemasc): If state is Connected, and the new candidates are for a
1264 // newly added transport, then the state actually _should_ move to
1265 // checking. Add a way to distinguish that case.
1266 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1267 }
1268 // TODO(bemasc): If state is Completed, go back to Connected.
1269 } else {
1270 LOG(LS_WARNING) << error;
1271 }
1272 return true;
1273}
1274
1275void WebRtcSession::RemoveUnusedChannelsAndTransports(
1276 const SessionDescription* desc) {
1277 const cricket::ContentInfo* voice_info =
1278 cricket::GetFirstAudioContent(desc);
1279 if ((!voice_info || voice_info->rejected) && voice_channel_) {
1280 mediastream_signaling_->OnAudioChannelClose();
1281 SignalVoiceChannelDestroyed();
1282 const std::string content_name = voice_channel_->content_name();
1283 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
1284 DestroyTransportProxy(content_name);
1285 }
1286
1287 const cricket::ContentInfo* video_info =
1288 cricket::GetFirstVideoContent(desc);
1289 if ((!video_info || video_info->rejected) && video_channel_) {
1290 mediastream_signaling_->OnVideoChannelClose();
1291 SignalVideoChannelDestroyed();
1292 const std::string content_name = video_channel_->content_name();
1293 channel_manager_->DestroyVideoChannel(video_channel_.release());
1294 DestroyTransportProxy(content_name);
1295 }
1296
1297 const cricket::ContentInfo* data_info =
1298 cricket::GetFirstDataContent(desc);
1299 if ((!data_info || data_info->rejected) && data_channel_) {
1300 mediastream_signaling_->OnDataChannelClose();
1301 SignalDataChannelDestroyed();
1302 const std::string content_name = data_channel_->content_name();
1303 channel_manager_->DestroyDataChannel(data_channel_.release());
1304 DestroyTransportProxy(content_name);
1305 }
1306}
1307
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001308// TODO(mallinath) - Add a correct error code if the channels are not creatued
1309// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001310bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
1311 // Disabling the BUNDLE flag in PortAllocator if offer disabled it.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001312 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1313 if (state() == STATE_INIT && !bundle_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001314 port_allocator()->set_flags(port_allocator()->flags() &
1315 ~cricket::PORTALLOCATOR_ENABLE_BUNDLE);
1316 }
1317
1318 // Creating the media channels and transport proxies.
1319 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1320 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001321 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322 LOG(LS_ERROR) << "Failed to create voice channel.";
1323 return false;
1324 }
1325 }
1326
1327 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1328 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001329 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001330 LOG(LS_ERROR) << "Failed to create video channel.";
1331 return false;
1332 }
1333 }
1334
1335 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1336 if (data_channel_type_ != cricket::DCT_NONE &&
1337 data && !data->rejected && !data_channel_.get()) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001338 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001339 LOG(LS_ERROR) << "Failed to create data channel.";
1340 return false;
1341 }
1342 }
1343
1344 return true;
1345}
1346
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001347bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001349 this, content->name, true));
wu@webrtc.orgde305012013-10-31 15:40:38 +00001350 if (!voice_channel_.get())
1351 return false;
1352
1353 if (dscp_enabled_) {
1354 cricket::AudioOptions options;
1355 options.dscp.Set(true);
1356 voice_channel_->SetChannelOptions(options);
1357 }
1358 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001359}
1360
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001361bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001362 video_channel_.reset(channel_manager_->CreateVideoChannel(
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001363 this, content->name, true, voice_channel_.get()));
wu@webrtc.orgde305012013-10-31 15:40:38 +00001364 if (!video_channel_.get())
1365 return false;
1366
1367 if (dscp_enabled_) {
1368 cricket::VideoOptions options;
1369 options.dscp.Set(true);
1370 video_channel_->SetChannelOptions(options);
1371 }
1372 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001373}
1374
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001375bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001376 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001377 data_channel_.reset(channel_manager_->CreateDataChannel(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001378 this, content->name, !sctp, data_channel_type_));
wu@webrtc.org91053e72013-08-10 07:18:04 +00001379 if (!data_channel_.get()) {
1380 return false;
1381 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001382 if (sctp) {
1383 mediastream_signaling_->OnDataTransportCreatedForSctp();
mallinath@webrtc.org0f3356e2014-01-11 01:26:23 +00001384 data_channel_->SignalDataReceived.connect(
1385 this, &WebRtcSession::OnDataChannelMessageReceived);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001386 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001387 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001388}
1389
1390void WebRtcSession::CopySavedCandidates(
1391 SessionDescriptionInterface* dest_desc) {
1392 if (!dest_desc) {
1393 ASSERT(false);
1394 return;
1395 }
1396 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
1397 dest_desc->AddCandidate(saved_candidates_[i]);
1398 delete saved_candidates_[i];
1399 }
1400 saved_candidates_.clear();
1401}
1402
mallinath@webrtc.org0f3356e2014-01-11 01:26:23 +00001403void WebRtcSession::OnDataChannelMessageReceived(
1404 cricket::DataChannel* channel,
1405 const cricket::ReceiveDataParams& params,
1406 const talk_base::Buffer& payload) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001407 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
mallinath@webrtc.org0f3356e2014-01-11 01:26:23 +00001408 if (params.type == cricket::DMT_CONTROL &&
1409 mediastream_signaling_->IsSctpSidAvailable(params.ssrc)) {
1410 // Received CONTROL on unused sid, process as an OPEN message.
1411 mediastream_signaling_->AddDataChannelFromOpenMessage(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001412 }
mallinath@webrtc.org0f3356e2014-01-11 01:26:23 +00001413 // otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001414}
1415
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001416// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001417bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001418 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1419 if (!bundle_enabled)
1420 return true;
1421
1422 const cricket::ContentGroup* bundle_group =
1423 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1424 ASSERT(bundle_group != NULL);
1425
1426 const cricket::ContentInfos& contents = desc->contents();
1427 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1428 citer != contents.end(); ++citer) {
1429 const cricket::ContentInfo* content = (&*citer);
1430 ASSERT(content != NULL);
1431 if (bundle_group->HasContentName(content->name) &&
1432 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1433 if (!HasRtcpMuxEnabled(content))
1434 return false;
1435 }
1436 }
1437 // RTCP-MUX is enabled in all the contents.
1438 return true;
1439}
1440
1441bool WebRtcSession::HasRtcpMuxEnabled(
1442 const cricket::ContentInfo* content) {
1443 const cricket::MediaContentDescription* description =
1444 static_cast<cricket::MediaContentDescription*>(content->description);
1445 return description->rtcp_mux();
1446}
1447
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001448bool WebRtcSession::ValidateSessionDescription(
1449 const SessionDescriptionInterface* sdesc,
1450 cricket::ContentSource source, std::string* error_desc) {
1451
1452 if (error() != cricket::BaseSession::ERROR_NONE) {
1453 return BadSdp(source, SessionErrorMsg(error()), error_desc);
1454 }
1455
1456 if (!sdesc || !sdesc->description()) {
1457 return BadSdp(source, kInvalidSdp, error_desc);
1458 }
1459
1460 std::string type = sdesc->type();
1461 Action action = GetAction(sdesc->type());
1462 if (source == cricket::CS_LOCAL) {
1463 if (!ExpectSetLocalDescription(action))
1464 return BadSdp(source, BadStateErrMsg(type, state()), error_desc);
1465 } else {
1466 if (!ExpectSetRemoteDescription(action))
1467 return BadSdp(source, BadStateErrMsg(type, state()), error_desc);
1468 }
1469
1470 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001471 std::string crypto_error;
wu@webrtc.org364f2042013-11-20 21:49:41 +00001472 if (webrtc_session_desc_factory_->Secure() == cricket::SEC_REQUIRED &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001473 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
1474 return BadSdp(source, crypto_error, error_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001475 }
1476
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001477 // Verify ice-ufrag and ice-pwd.
1478 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
1479 return BadSdp(source, kSdpWithoutIceUfragPwd, error_desc);
1480 }
1481
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001482 if (!ValidateBundleSettings(sdesc->description())) {
1483 return BadSdp(source, kBundleWithoutRtcpMux, error_desc);
1484 }
1485
1486 // Verify m-lines in Answer when compared against Offer.
1487 if (action == kAnswer) {
1488 const cricket::SessionDescription* offer_desc =
1489 (source == cricket::CS_LOCAL) ? remote_description()->description() :
1490 local_description()->description();
1491 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
1492 return BadSdp(source, kMlineMismatch, error_desc);
1493 }
1494 }
1495
1496 return true;
1497}
1498
1499bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1500 return ((action == kOffer && state() == STATE_INIT) ||
1501 // update local offer
1502 (action == kOffer && state() == STATE_SENTINITIATE) ||
1503 // update the current ongoing session.
1504 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1505 (action == kOffer && state() == STATE_SENTACCEPT) ||
1506 (action == kOffer && state() == STATE_INPROGRESS) ||
1507 // accept remote offer
1508 (action == kAnswer && state() == STATE_RECEIVEDINITIATE) ||
1509 (action == kAnswer && state() == STATE_SENTPRACCEPT) ||
1510 (action == kPrAnswer && state() == STATE_RECEIVEDINITIATE) ||
1511 (action == kPrAnswer && state() == STATE_SENTPRACCEPT));
1512}
1513
1514bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1515 return ((action == kOffer && state() == STATE_INIT) ||
1516 // update remote offer
1517 (action == kOffer && state() == STATE_RECEIVEDINITIATE) ||
1518 // update the current ongoing session
1519 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1520 (action == kOffer && state() == STATE_SENTACCEPT) ||
1521 (action == kOffer && state() == STATE_INPROGRESS) ||
1522 // accept local offer
1523 (action == kAnswer && state() == STATE_SENTINITIATE) ||
1524 (action == kAnswer && state() == STATE_RECEIVEDPRACCEPT) ||
1525 (action == kPrAnswer && state() == STATE_SENTINITIATE) ||
1526 (action == kPrAnswer && state() == STATE_RECEIVEDPRACCEPT));
1527}
1528
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001529} // namespace webrtc