blob: 59d72709f33f81d7ea39358b3139a6382979343c [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
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000058const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
59 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000060const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061const char kInvalidCandidates[] = "Description contains invalid candidates.";
62const char kInvalidSdp[] = "Invalid session description.";
63const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000064 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
65const char kPushDownTDFailed[] =
66 "Failed to push down transport description:";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067const char kSdpWithoutCrypto[] = "Called with a SDP without crypto enabled.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000068const char kSdpWithoutIceUfragPwd[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000069 "Called with a SDP without ice-ufrag and ice-pwd.";
70const char kSdpWithoutSdesAndDtlsDisabled[] =
71 "Called with a SDP without SDES crypto and DTLS disabled locally.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000073const char kSessionErrorDesc[] = "Session error description: ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074
75// Compares |answer| against |offer|. Comparision is done
76// for number of m-lines in answer against offer. If matches true will be
77// returned otherwise false.
78static bool VerifyMediaDescriptions(
79 const SessionDescription* answer, const SessionDescription* offer) {
80 if (offer->contents().size() != answer->contents().size())
81 return false;
82
83 for (size_t i = 0; i < offer->contents().size(); ++i) {
84 if ((offer->contents()[i].name) != answer->contents()[i].name) {
85 return false;
86 }
87 }
88 return true;
89}
90
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091// Checks that each non-rejected content has SDES crypto keys or a DTLS
92// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
93// keys, will be caught in Transport negotiation, and backstopped by Channel's
94// |secure_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000095static bool VerifyCrypto(const SessionDescription* desc,
96 bool dtls_enabled,
97 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 const ContentInfos& contents = desc->contents();
99 for (size_t index = 0; index < contents.size(); ++index) {
100 const ContentInfo* cinfo = &contents[index];
101 if (cinfo->rejected) {
102 continue;
103 }
104
105 // If the content isn't rejected, crypto must be present.
106 const MediaContentDescription* media =
107 static_cast<const MediaContentDescription*>(cinfo->description);
108 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
109 if (!media || !tinfo) {
110 // Something is not right.
111 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000112 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 return false;
114 }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000115 if (media->cryptos().empty()) {
116 if (!tinfo->description.identity_fingerprint) {
117 // Crypto must be supplied.
118 LOG(LS_WARNING) << "Session description must have SDES or DTLS-SRTP.";
119 *error = kSdpWithoutCrypto;
120 return false;
121 }
122 if (!dtls_enabled) {
123 LOG(LS_WARNING) <<
124 "Session description must have SDES when DTLS disabled.";
125 *error = kSdpWithoutSdesAndDtlsDisabled;
126 return false;
127 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 }
129 }
130
131 return true;
132}
133
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000134// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
135static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
136 const ContentInfos& contents = desc->contents();
137 for (size_t index = 0; index < contents.size(); ++index) {
138 const ContentInfo* cinfo = &contents[index];
139 if (cinfo->rejected) {
140 continue;
141 }
142
143 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
144 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
145 if (!tinfo) {
146 // Something is not right.
147 LOG(LS_ERROR) << kInvalidSdp;
148 return false;
149 }
150 if (tinfo->description.ice_ufrag.empty() ||
151 tinfo->description.ice_pwd.empty()) {
152 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
153 return false;
154 }
155 }
156 return true;
157}
158
wu@webrtc.org91053e72013-08-10 07:18:04 +0000159// Forces |sdesc->crypto_required| to the appropriate state based on the
160// current security policy, to ensure a failure occurs if there is an error
161// in crypto negotiation.
162// Called when processing the local session description.
163static void UpdateSessionDescriptionSecurePolicy(
164 cricket::SecureMediaPolicy secure_policy,
165 SessionDescription* sdesc) {
166 if (!sdesc) {
167 return;
168 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169
wu@webrtc.org91053e72013-08-10 07:18:04 +0000170 // Updating the |crypto_required_| in MediaContentDescription to the
171 // appropriate state based on the current security policy.
172 for (cricket::ContentInfos::iterator iter = sdesc->contents().begin();
173 iter != sdesc->contents().end(); ++iter) {
174 if (cricket::IsMediaContent(&*iter)) {
175 MediaContentDescription* mdesc =
176 static_cast<MediaContentDescription*> (iter->description);
177 if (mdesc) {
178 mdesc->set_crypto_required(secure_policy == cricket::SEC_REQUIRED);
179 }
180 }
181 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182}
183
184static bool GetAudioSsrcByTrackId(
185 const SessionDescription* session_description,
186 const std::string& track_id, uint32 *ssrc) {
187 const cricket::ContentInfo* audio_info =
188 cricket::GetFirstAudioContent(session_description);
189 if (!audio_info) {
190 LOG(LS_ERROR) << "Audio not used in this call";
191 return false;
192 }
193
194 const cricket::MediaContentDescription* audio_content =
195 static_cast<const cricket::MediaContentDescription*>(
196 audio_info->description);
197 cricket::StreamParams stream;
198 if (!cricket::GetStreamByIds(audio_content->streams(), "", track_id,
199 &stream)) {
200 return false;
201 }
202 *ssrc = stream.first_ssrc();
203 return true;
204}
205
206static bool GetTrackIdBySsrc(const SessionDescription* session_description,
207 uint32 ssrc, std::string* track_id) {
208 ASSERT(track_id != NULL);
209
210 cricket::StreamParams stream_out;
211 const cricket::ContentInfo* audio_info =
212 cricket::GetFirstAudioContent(session_description);
213 if (!audio_info) {
214 return false;
215 }
216 const cricket::MediaContentDescription* audio_content =
217 static_cast<const cricket::MediaContentDescription*>(
218 audio_info->description);
219
220 if (cricket::GetStreamBySsrc(audio_content->streams(), ssrc, &stream_out)) {
221 *track_id = stream_out.id;
222 return true;
223 }
224
225 const cricket::ContentInfo* video_info =
226 cricket::GetFirstVideoContent(session_description);
227 if (!video_info) {
228 return false;
229 }
230 const cricket::MediaContentDescription* video_content =
231 static_cast<const cricket::MediaContentDescription*>(
232 video_info->description);
233
234 if (cricket::GetStreamBySsrc(video_content->streams(), ssrc, &stream_out)) {
235 *track_id = stream_out.id;
236 return true;
237 }
238 return false;
239}
240
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000241static bool BadSdp(const std::string& source,
242 const std::string& type,
243 const std::string& reason,
244 std::string* err_desc) {
245 std::ostringstream desc;
246 desc << "Failed to set " << source << " " << type << " sdp: " << reason;
247
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000249 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000251 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252 return false;
253}
254
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000256 const std::string& type,
257 const std::string& reason,
258 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000260 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000262 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263 }
264}
265
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000266static bool BadLocalSdp(const std::string& type,
267 const std::string& reason,
268 std::string* err_desc) {
269 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
270}
271
272static bool BadRemoteSdp(const std::string& type,
273 const std::string& reason,
274 std::string* err_desc) {
275 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
276}
277
278static bool BadOfferSdp(cricket::ContentSource source,
279 const std::string& reason,
280 std::string* err_desc) {
281 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
282}
283
284static bool BadPranswerSdp(cricket::ContentSource source,
285 const std::string& reason,
286 std::string* err_desc) {
287 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
288 reason, err_desc);
289}
290
291static bool BadAnswerSdp(cricket::ContentSource source,
292 const std::string& reason,
293 std::string* err_desc) {
294 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295}
296
297#define GET_STRING_OF_STATE(state) \
298 case cricket::BaseSession::state: \
299 result = #state; \
300 break;
301
302static std::string GetStateString(cricket::BaseSession::State state) {
303 std::string result;
304 switch (state) {
305 GET_STRING_OF_STATE(STATE_INIT)
306 GET_STRING_OF_STATE(STATE_SENTINITIATE)
307 GET_STRING_OF_STATE(STATE_RECEIVEDINITIATE)
308 GET_STRING_OF_STATE(STATE_SENTPRACCEPT)
309 GET_STRING_OF_STATE(STATE_SENTACCEPT)
310 GET_STRING_OF_STATE(STATE_RECEIVEDPRACCEPT)
311 GET_STRING_OF_STATE(STATE_RECEIVEDACCEPT)
312 GET_STRING_OF_STATE(STATE_SENTMODIFY)
313 GET_STRING_OF_STATE(STATE_RECEIVEDMODIFY)
314 GET_STRING_OF_STATE(STATE_SENTREJECT)
315 GET_STRING_OF_STATE(STATE_RECEIVEDREJECT)
316 GET_STRING_OF_STATE(STATE_SENTREDIRECT)
317 GET_STRING_OF_STATE(STATE_SENTTERMINATE)
318 GET_STRING_OF_STATE(STATE_RECEIVEDTERMINATE)
319 GET_STRING_OF_STATE(STATE_INPROGRESS)
320 GET_STRING_OF_STATE(STATE_DEINIT)
321 default:
322 ASSERT(false);
323 break;
324 }
325 return result;
326}
327
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000328#define GET_STRING_OF_ERROR_CODE(err) \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329 case cricket::BaseSession::err: \
330 result = #err; \
331 break;
332
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000333static std::string GetErrorCodeString(cricket::BaseSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000334 std::string result;
335 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000336 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
337 GET_STRING_OF_ERROR_CODE(ERROR_TIME)
338 GET_STRING_OF_ERROR_CODE(ERROR_RESPONSE)
339 GET_STRING_OF_ERROR_CODE(ERROR_NETWORK)
340 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
341 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342 default:
343 ASSERT(false);
344 break;
345 }
346 return result;
347}
348
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000349static std::string MakeErrorString(const std::string& error,
350 const std::string& desc) {
351 std::ostringstream ret;
352 ret << error << " " << desc;
353 return ret.str();
354}
355
356static std::string MakeTdErrorString(const std::string& desc) {
357 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358}
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) {
wu@webrtc.org364f2042013-11-20 21:49:41 +0000529 webrtc_session_desc_factory_->SetSecure(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000530 }
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
wu@webrtc.org364f2042013-11-20 21:49:41 +0000557void WebRtcSession::SetSecurePolicy(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000558 cricket::SecureMediaPolicy secure_policy) {
wu@webrtc.org364f2042013-11-20 21:49:41 +0000559 webrtc_session_desc_factory_->SetSecure(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000560}
561
wu@webrtc.org364f2042013-11-20 21:49:41 +0000562cricket::SecureMediaPolicy WebRtcSession::SecurePolicy() 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 =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000613 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.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000624 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000625 }
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
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000631 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632 return false;
633 }
634 // Kick starting the ice candidates allocation.
635 StartCandidatesAllocation();
636
637 // Update state and SSRC of local MediaStreams and DataChannels based on the
638 // local session description.
639 mediastream_signaling_->OnLocalDescriptionChanged(local_desc_.get());
640
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000641 talk_base::SSLRole role;
642 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
643 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
644 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000646 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647 }
648 return true;
649}
650
651bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
652 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000653 // Takes the ownership of |desc| regardless of the result.
654 talk_base::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
655
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000656 // Validate SDP.
657 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
658 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 }
660
661 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000662 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000663 if (action == kOffer && !CreateChannels(desc->description())) {
664 // TODO(mallinath) - Handle CreateChannel failure, as new local description
665 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000666 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667 }
668
669 // Remove channel and transport proxies, if MediaContentDescription is
670 // rejected.
671 RemoveUnusedChannelsAndTransports(desc->description());
672
673 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
674 // is called.
675 set_remote_description(desc->description()->Copy());
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000676 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677 return false;
678 }
679
680 // Update remote MediaStreams.
681 mediastream_signaling_->OnRemoteDescriptionChanged(desc);
682 if (local_description() && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000683 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000684 }
685
686 // Copy all saved candidates.
687 CopySavedCandidates(desc);
688 // We retain all received candidates.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000689 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
690 remote_desc_.get(), desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000691 // Check if this new SessionDescription contains new ice ufrag and password
692 // that indicates the remote peer requests ice restart.
693 ice_restart_latch_->CheckForRemoteIceRestart(remote_desc_.get(),
694 desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000695 remote_desc_.reset(desc_temp.release());
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000696
697 talk_base::SSLRole role;
698 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
699 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
700 }
701
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000703 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000704 }
705 return true;
706}
707
708bool WebRtcSession::UpdateSessionState(
709 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710 std::string* err_desc) {
711 // If there's already a pending error then no state transition should happen.
712 // But all call-sites should be verifying this before calling us!
713 ASSERT(error() == cricket::BaseSession::ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000714 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000716 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
717 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718 }
719 SetState(source == cricket::CS_LOCAL ?
720 STATE_SENTINITIATE : STATE_RECEIVEDINITIATE);
721 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000722 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 }
724 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000725 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
726 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727 }
728 EnableChannels();
729 SetState(source == cricket::CS_LOCAL ?
730 STATE_SENTPRACCEPT : STATE_RECEIVEDPRACCEPT);
731 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000732 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733 }
734 } else if (action == kAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000735 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
736 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000737 }
738 MaybeEnableMuxingSupport();
739 EnableChannels();
740 SetState(source == cricket::CS_LOCAL ?
741 STATE_SENTACCEPT : STATE_RECEIVEDACCEPT);
742 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000743 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 }
745 }
746 return true;
747}
748
749WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
750 if (type == SessionDescriptionInterface::kOffer) {
751 return WebRtcSession::kOffer;
752 } else if (type == SessionDescriptionInterface::kPrAnswer) {
753 return WebRtcSession::kPrAnswer;
754 } else if (type == SessionDescriptionInterface::kAnswer) {
755 return WebRtcSession::kAnswer;
756 }
757 ASSERT(false && "unknown action type");
758 return WebRtcSession::kOffer;
759}
760
761bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
762 if (state() == STATE_INIT) {
763 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
764 << "without any offer (local or remote) "
765 << "session description.";
766 return false;
767 }
768
769 if (!candidate) {
770 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL";
771 return false;
772 }
773
774 if (!local_description() || !remote_description()) {
775 LOG(LS_INFO) << "ProcessIceMessage: Remote description not set, "
776 << "save the candidate for later use.";
777 saved_candidates_.push_back(
778 new JsepIceCandidate(candidate->sdp_mid(), candidate->sdp_mline_index(),
779 candidate->candidate()));
780 return true;
781 }
782
783 // Add this candidate to the remote session description.
784 if (!remote_desc_->AddCandidate(candidate)) {
785 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used";
786 return false;
787 }
788
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000789 return UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790}
791
792bool WebRtcSession::GetTrackIdBySsrc(uint32 ssrc, std::string* id) {
793 if (GetLocalTrackId(ssrc, id)) {
794 if (GetRemoteTrackId(ssrc, id)) {
795 LOG(LS_WARNING) << "SSRC " << ssrc
796 << " exists in both local and remote descriptions";
797 return true; // We return the remote track id.
798 }
799 return true;
800 } else {
801 return GetRemoteTrackId(ssrc, id);
802 }
803}
804
805bool WebRtcSession::GetLocalTrackId(uint32 ssrc, std::string* track_id) {
806 if (!BaseSession::local_description())
807 return false;
808 return webrtc::GetTrackIdBySsrc(
809 BaseSession::local_description(), ssrc, track_id);
810}
811
812bool WebRtcSession::GetRemoteTrackId(uint32 ssrc, std::string* track_id) {
813 if (!BaseSession::remote_description())
814 return false;
815 return webrtc::GetTrackIdBySsrc(
816 BaseSession::remote_description(), ssrc, track_id);
817}
818
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000819std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000821 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 return desc.str();
823}
824
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000825void WebRtcSession::SetAudioPlayout(uint32 ssrc, bool enable,
826 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 ASSERT(signaling_thread()->IsCurrent());
828 if (!voice_channel_) {
829 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
830 return;
831 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000832 if (!voice_channel_->SetRemoteRenderer(ssrc, renderer)) {
833 // SetRenderer() can fail if the ssrc does not match any playout channel.
834 LOG(LS_ERROR) << "SetAudioPlayout: ssrc is incorrect: " << ssrc;
835 return;
836 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 if (!voice_channel_->SetOutputScaling(ssrc, enable ? 1 : 0, enable ? 1 : 0)) {
838 // Allow that SetOutputScaling fail if |enable| is false but assert
839 // otherwise. This in the normal case when the underlying media channel has
840 // already been deleted.
841 ASSERT(enable == false);
842 }
843}
844
845void WebRtcSession::SetAudioSend(uint32 ssrc, bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000846 const cricket::AudioOptions& options,
847 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 ASSERT(signaling_thread()->IsCurrent());
849 if (!voice_channel_) {
850 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
851 return;
852 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000853 if (!voice_channel_->SetLocalRenderer(ssrc, renderer)) {
854 // SetRenderer() can fail if the ssrc does not match any send channel.
855 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
856 return;
857 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 if (!voice_channel_->MuteStream(ssrc, !enable)) {
859 // Allow that MuteStream fail if |enable| is false but assert otherwise.
860 // This in the normal case when the underlying media channel has already
861 // been deleted.
862 ASSERT(enable == false);
863 return;
864 }
865 if (enable)
866 voice_channel_->SetChannelOptions(options);
867}
868
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869bool WebRtcSession::SetCaptureDevice(uint32 ssrc,
870 cricket::VideoCapturer* camera) {
871 ASSERT(signaling_thread()->IsCurrent());
872
873 if (!video_channel_.get()) {
874 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
875 // support video.
876 LOG(LS_WARNING) << "Video not used in this call.";
877 return false;
878 }
879 if (!video_channel_->SetCapturer(ssrc, camera)) {
880 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
881 // This in the normal case when the underlying media channel has already
882 // been deleted.
883 ASSERT(camera == NULL);
884 return false;
885 }
886 return true;
887}
888
889void WebRtcSession::SetVideoPlayout(uint32 ssrc,
890 bool enable,
891 cricket::VideoRenderer* renderer) {
892 ASSERT(signaling_thread()->IsCurrent());
893 if (!video_channel_) {
894 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
895 return;
896 }
897 if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) {
898 // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise.
899 // This in the normal case when the underlying media channel has already
900 // been deleted.
901 ASSERT(renderer == NULL);
902 }
903}
904
905void WebRtcSession::SetVideoSend(uint32 ssrc, bool enable,
906 const cricket::VideoOptions* options) {
907 ASSERT(signaling_thread()->IsCurrent());
908 if (!video_channel_) {
909 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
910 return;
911 }
912 if (!video_channel_->MuteStream(ssrc, !enable)) {
913 // Allow that MuteStream fail if |enable| is false but assert otherwise.
914 // This in the normal case when the underlying media channel has already
915 // been deleted.
916 ASSERT(enable == false);
917 return;
918 }
919 if (enable && options)
920 video_channel_->SetChannelOptions(*options);
921}
922
923bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
924 ASSERT(signaling_thread()->IsCurrent());
925 if (!voice_channel_) {
926 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
927 return false;
928 }
929 uint32 send_ssrc = 0;
930 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
931 // exists.
932 if (!GetAudioSsrcByTrackId(BaseSession::local_description(), track_id,
933 &send_ssrc)) {
934 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
935 return false;
936 }
937 return voice_channel_->CanInsertDtmf();
938}
939
940bool WebRtcSession::InsertDtmf(const std::string& track_id,
941 int code, int duration) {
942 ASSERT(signaling_thread()->IsCurrent());
943 if (!voice_channel_) {
944 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
945 return false;
946 }
947 uint32 send_ssrc = 0;
948 if (!VERIFY(GetAudioSsrcByTrackId(BaseSession::local_description(),
949 track_id, &send_ssrc))) {
950 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
951 return false;
952 }
953 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration,
954 cricket::DF_SEND)) {
955 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
956 return false;
957 }
958 return true;
959}
960
961sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
962 return &SignalVoiceChannelDestroyed;
963}
964
wu@webrtc.org78187522013-10-07 23:32:02 +0000965bool WebRtcSession::SendData(const cricket::SendDataParams& params,
966 const talk_base::Buffer& payload,
967 cricket::SendDataResult* result) {
968 if (!data_channel_.get()) {
969 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
970 return false;
971 }
972 return data_channel_->SendData(params, payload, result);
973}
974
975bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
976 if (!data_channel_.get()) {
977 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
978 return false;
979 }
wu@webrtc.org78187522013-10-07 23:32:02 +0000980 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
981 &DataChannel::OnChannelReady);
982 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
983 &DataChannel::OnDataReceived);
wu@webrtc.org78187522013-10-07 23:32:02 +0000984 return true;
985}
986
987void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000988 if (!data_channel_.get()) {
989 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
990 return;
991 }
wu@webrtc.org78187522013-10-07 23:32:02 +0000992 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
993 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
994}
995
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000996void WebRtcSession::AddSctpDataStream(uint32 sid) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000997 if (!data_channel_.get()) {
998 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
999 return;
1000 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001001 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1002 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001003}
1004
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001005void WebRtcSession::RemoveSctpDataStream(uint32 sid) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001006 if (!data_channel_.get()) {
1007 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1008 << "NULL.";
1009 return;
1010 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001011 data_channel_->RemoveRecvStream(sid);
1012 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001013}
1014
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001015bool WebRtcSession::ReadyToSendData() const {
1016 return data_channel_.get() && data_channel_->ready_to_send_data();
1017}
1018
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001019talk_base::scoped_refptr<DataChannel> WebRtcSession::CreateDataChannel(
wu@webrtc.org78187522013-10-07 23:32:02 +00001020 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001021 const InternalDataChannelInit* config) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001022 if (state() == STATE_RECEIVEDTERMINATE) {
1023 return NULL;
1024 }
1025 if (data_channel_type_ == cricket::DCT_NONE) {
1026 LOG(LS_ERROR) << "CreateDataChannel: Data is not supported in this call.";
1027 return NULL;
1028 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001029 InternalDataChannelInit new_config =
1030 config ? (*config) : InternalDataChannelInit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001031 if (data_channel_type_ == cricket::DCT_SCTP) {
1032 if (new_config.id < 0) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001033 talk_base::SSLRole role;
1034 if (GetSslRole(&role) &&
1035 !mediastream_signaling_->AllocateSctpSid(role, &new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001036 LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel.";
1037 return NULL;
1038 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001039 } else if (!mediastream_signaling_->IsSctpSidAvailable(new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001040 LOG(LS_ERROR) << "Failed to create a SCTP data channel "
1041 << "because the id is already in use or out of range.";
1042 return NULL;
1043 }
1044 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001045
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001046 talk_base::scoped_refptr<DataChannel> channel(DataChannel::Create(
1047 this, data_channel_type_, label, new_config));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001048 if (channel && !mediastream_signaling_->AddDataChannel(channel))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001049 return NULL;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001050
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001051 return channel;
1052}
1053
1054cricket::DataChannelType WebRtcSession::data_channel_type() const {
1055 return data_channel_type_;
1056}
1057
wu@webrtc.org91053e72013-08-10 07:18:04 +00001058bool WebRtcSession::IceRestartPending() const {
1059 return ice_restart_latch_->Get();
1060}
1061
1062void WebRtcSession::ResetIceRestartLatch() {
1063 ice_restart_latch_->Reset();
1064}
1065
1066void WebRtcSession::OnIdentityReady(talk_base::SSLIdentity* identity) {
1067 SetIdentity(identity);
1068}
1069
1070bool WebRtcSession::waiting_for_identity() const {
1071 return webrtc_session_desc_factory_->waiting_for_identity();
1072}
1073
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001074void WebRtcSession::SetIceConnectionState(
1075 PeerConnectionInterface::IceConnectionState state) {
1076 if (ice_connection_state_ == state) {
1077 return;
1078 }
1079
1080 // ASSERT that the requested transition is allowed. Note that
1081 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1082 // within PeerConnection). This switch statement should compile away when
1083 // ASSERTs are disabled.
1084 switch (ice_connection_state_) {
1085 case PeerConnectionInterface::kIceConnectionNew:
1086 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1087 break;
1088 case PeerConnectionInterface::kIceConnectionChecking:
1089 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1090 state == PeerConnectionInterface::kIceConnectionConnected);
1091 break;
1092 case PeerConnectionInterface::kIceConnectionConnected:
1093 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1094 state == PeerConnectionInterface::kIceConnectionChecking ||
1095 state == PeerConnectionInterface::kIceConnectionCompleted);
1096 break;
1097 case PeerConnectionInterface::kIceConnectionCompleted:
1098 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1099 state == PeerConnectionInterface::kIceConnectionDisconnected);
1100 break;
1101 case PeerConnectionInterface::kIceConnectionFailed:
1102 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1103 break;
1104 case PeerConnectionInterface::kIceConnectionDisconnected:
1105 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1106 state == PeerConnectionInterface::kIceConnectionConnected ||
1107 state == PeerConnectionInterface::kIceConnectionCompleted ||
1108 state == PeerConnectionInterface::kIceConnectionFailed);
1109 break;
1110 case PeerConnectionInterface::kIceConnectionClosed:
1111 ASSERT(false);
1112 break;
1113 default:
1114 ASSERT(false);
1115 break;
1116 }
1117
1118 ice_connection_state_ = state;
1119 if (ice_observer_) {
1120 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1121 }
1122}
1123
1124void WebRtcSession::OnTransportRequestSignaling(
1125 cricket::Transport* transport) {
1126 ASSERT(signaling_thread()->IsCurrent());
1127 transport->OnSignalingReady();
1128 if (ice_observer_) {
1129 ice_observer_->OnIceGatheringChange(
1130 PeerConnectionInterface::kIceGatheringGathering);
1131 }
1132}
1133
1134void WebRtcSession::OnTransportConnecting(cricket::Transport* transport) {
1135 ASSERT(signaling_thread()->IsCurrent());
1136 // start monitoring for the write state of the transport.
1137 OnTransportWritable(transport);
1138}
1139
1140void WebRtcSession::OnTransportWritable(cricket::Transport* transport) {
1141 ASSERT(signaling_thread()->IsCurrent());
1142 // TODO(bemasc): Expose more API from Transport to detect when
1143 // candidate selection starts or stops, due to success or failure.
1144 if (transport->all_channels_writable()) {
1145 if (ice_connection_state_ ==
1146 PeerConnectionInterface::kIceConnectionChecking ||
1147 ice_connection_state_ ==
1148 PeerConnectionInterface::kIceConnectionDisconnected) {
1149 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1150 }
1151 } else if (transport->HasChannels()) {
1152 // If the current state is Connected or Completed, then there were writable
1153 // channels but now there are not, so the next state must be Disconnected.
1154 if (ice_connection_state_ ==
1155 PeerConnectionInterface::kIceConnectionConnected ||
1156 ice_connection_state_ ==
1157 PeerConnectionInterface::kIceConnectionCompleted) {
1158 SetIceConnectionState(
1159 PeerConnectionInterface::kIceConnectionDisconnected);
1160 }
1161 }
1162}
1163
1164void WebRtcSession::OnTransportProxyCandidatesReady(
1165 cricket::TransportProxy* proxy, const cricket::Candidates& candidates) {
1166 ASSERT(signaling_thread()->IsCurrent());
1167 ProcessNewLocalCandidate(proxy->content_name(), candidates);
1168}
1169
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001170void WebRtcSession::OnCandidatesAllocationDone() {
1171 ASSERT(signaling_thread()->IsCurrent());
1172 if (ice_observer_) {
1173 ice_observer_->OnIceGatheringChange(
1174 PeerConnectionInterface::kIceGatheringComplete);
1175 ice_observer_->OnIceComplete();
1176 }
1177}
1178
1179// Enabling voice and video channel.
1180void WebRtcSession::EnableChannels() {
1181 if (voice_channel_ && !voice_channel_->enabled())
1182 voice_channel_->Enable(true);
1183
1184 if (video_channel_ && !video_channel_->enabled())
1185 video_channel_->Enable(true);
1186
1187 if (data_channel_.get() && !data_channel_->enabled())
1188 data_channel_->Enable(true);
1189}
1190
1191void WebRtcSession::ProcessNewLocalCandidate(
1192 const std::string& content_name,
1193 const cricket::Candidates& candidates) {
1194 int sdp_mline_index;
1195 if (!GetLocalCandidateMediaIndex(content_name, &sdp_mline_index)) {
1196 LOG(LS_ERROR) << "ProcessNewLocalCandidate: content name "
1197 << content_name << " not found";
1198 return;
1199 }
1200
1201 for (cricket::Candidates::const_iterator citer = candidates.begin();
1202 citer != candidates.end(); ++citer) {
1203 // Use content_name as the candidate media id.
1204 JsepIceCandidate candidate(content_name, sdp_mline_index, *citer);
1205 if (ice_observer_) {
1206 ice_observer_->OnIceCandidate(&candidate);
1207 }
1208 if (local_desc_) {
1209 local_desc_->AddCandidate(&candidate);
1210 }
1211 }
1212}
1213
1214// Returns the media index for a local ice candidate given the content name.
1215bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1216 int* sdp_mline_index) {
1217 if (!BaseSession::local_description() || !sdp_mline_index)
1218 return false;
1219
1220 bool content_found = false;
1221 const ContentInfos& contents = BaseSession::local_description()->contents();
1222 for (size_t index = 0; index < contents.size(); ++index) {
1223 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001224 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001225 content_found = true;
1226 break;
1227 }
1228 }
1229 return content_found;
1230}
1231
1232bool WebRtcSession::UseCandidatesInSessionDescription(
1233 const SessionDescriptionInterface* remote_desc) {
1234 if (!remote_desc)
1235 return true;
1236 bool ret = true;
1237 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1238 const IceCandidateCollection* candidates = remote_desc->candidates(m);
1239 for (size_t n = 0; n < candidates->count(); ++n) {
1240 ret = UseCandidate(candidates->at(n));
1241 if (!ret)
1242 break;
1243 }
1244 }
1245 return ret;
1246}
1247
1248bool WebRtcSession::UseCandidate(
1249 const IceCandidateInterface* candidate) {
1250
1251 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
1252 size_t remote_content_size =
1253 BaseSession::remote_description()->contents().size();
1254 if (mediacontent_index >= remote_content_size) {
1255 LOG(LS_ERROR)
1256 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1257 return false;
1258 }
1259
1260 cricket::ContentInfo content =
1261 BaseSession::remote_description()->contents()[mediacontent_index];
1262 std::vector<cricket::Candidate> candidates;
1263 candidates.push_back(candidate->candidate());
1264 // Invoking BaseSession method to handle remote candidates.
1265 std::string error;
1266 if (OnRemoteCandidates(content.name, candidates, &error)) {
1267 // Candidates successfully submitted for checking.
1268 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1269 ice_connection_state_ ==
1270 PeerConnectionInterface::kIceConnectionDisconnected) {
1271 // If state is New, then the session has just gotten its first remote ICE
1272 // candidates, so go to Checking.
1273 // If state is Disconnected, the session is re-using old candidates or
1274 // receiving additional ones, so go to Checking.
1275 // If state is Connected, stay Connected.
1276 // TODO(bemasc): If state is Connected, and the new candidates are for a
1277 // newly added transport, then the state actually _should_ move to
1278 // checking. Add a way to distinguish that case.
1279 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1280 }
1281 // TODO(bemasc): If state is Completed, go back to Connected.
1282 } else {
1283 LOG(LS_WARNING) << error;
1284 }
1285 return true;
1286}
1287
1288void WebRtcSession::RemoveUnusedChannelsAndTransports(
1289 const SessionDescription* desc) {
1290 const cricket::ContentInfo* voice_info =
1291 cricket::GetFirstAudioContent(desc);
1292 if ((!voice_info || voice_info->rejected) && voice_channel_) {
1293 mediastream_signaling_->OnAudioChannelClose();
1294 SignalVoiceChannelDestroyed();
1295 const std::string content_name = voice_channel_->content_name();
1296 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
1297 DestroyTransportProxy(content_name);
1298 }
1299
1300 const cricket::ContentInfo* video_info =
1301 cricket::GetFirstVideoContent(desc);
1302 if ((!video_info || video_info->rejected) && video_channel_) {
1303 mediastream_signaling_->OnVideoChannelClose();
1304 SignalVideoChannelDestroyed();
1305 const std::string content_name = video_channel_->content_name();
1306 channel_manager_->DestroyVideoChannel(video_channel_.release());
1307 DestroyTransportProxy(content_name);
1308 }
1309
1310 const cricket::ContentInfo* data_info =
1311 cricket::GetFirstDataContent(desc);
1312 if ((!data_info || data_info->rejected) && data_channel_) {
1313 mediastream_signaling_->OnDataChannelClose();
1314 SignalDataChannelDestroyed();
1315 const std::string content_name = data_channel_->content_name();
1316 channel_manager_->DestroyDataChannel(data_channel_.release());
1317 DestroyTransportProxy(content_name);
1318 }
1319}
1320
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001321// TODO(mallinath) - Add a correct error code if the channels are not creatued
1322// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001323bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
1324 // Disabling the BUNDLE flag in PortAllocator if offer disabled it.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001325 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1326 if (state() == STATE_INIT && !bundle_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001327 port_allocator()->set_flags(port_allocator()->flags() &
1328 ~cricket::PORTALLOCATOR_ENABLE_BUNDLE);
1329 }
1330
1331 // Creating the media channels and transport proxies.
1332 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1333 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001334 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001335 LOG(LS_ERROR) << "Failed to create voice channel.";
1336 return false;
1337 }
1338 }
1339
1340 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1341 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001342 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001343 LOG(LS_ERROR) << "Failed to create video channel.";
1344 return false;
1345 }
1346 }
1347
1348 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1349 if (data_channel_type_ != cricket::DCT_NONE &&
1350 data && !data->rejected && !data_channel_.get()) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001351 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001352 LOG(LS_ERROR) << "Failed to create data channel.";
1353 return false;
1354 }
1355 }
1356
1357 return true;
1358}
1359
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001360bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001361 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001362 this, content->name, true));
wu@webrtc.orgde305012013-10-31 15:40:38 +00001363 if (!voice_channel_.get())
1364 return false;
1365
1366 if (dscp_enabled_) {
1367 cricket::AudioOptions options;
1368 options.dscp.Set(true);
1369 voice_channel_->SetChannelOptions(options);
1370 }
1371 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001372}
1373
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001374bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001375 video_channel_.reset(channel_manager_->CreateVideoChannel(
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001376 this, content->name, true, voice_channel_.get()));
wu@webrtc.orgde305012013-10-31 15:40:38 +00001377 if (!video_channel_.get())
1378 return false;
1379
1380 if (dscp_enabled_) {
1381 cricket::VideoOptions options;
1382 options.dscp.Set(true);
1383 video_channel_->SetChannelOptions(options);
1384 }
1385 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001386}
1387
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001388bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001389 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001390 data_channel_.reset(channel_manager_->CreateDataChannel(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001391 this, content->name, !sctp, data_channel_type_));
wu@webrtc.org91053e72013-08-10 07:18:04 +00001392 if (!data_channel_.get()) {
1393 return false;
1394 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001395 if (sctp) {
1396 mediastream_signaling_->OnDataTransportCreatedForSctp();
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001397 data_channel_->SignalDataReceived.connect(
1398 this, &WebRtcSession::OnDataChannelMessageReceived);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001399 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001400 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001401}
1402
1403void WebRtcSession::CopySavedCandidates(
1404 SessionDescriptionInterface* dest_desc) {
1405 if (!dest_desc) {
1406 ASSERT(false);
1407 return;
1408 }
1409 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
1410 dest_desc->AddCandidate(saved_candidates_[i]);
1411 delete saved_candidates_[i];
1412 }
1413 saved_candidates_.clear();
1414}
1415
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001416void WebRtcSession::OnDataChannelMessageReceived(
1417 cricket::DataChannel* channel,
1418 const cricket::ReceiveDataParams& params,
1419 const talk_base::Buffer& payload) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001420 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001421 if (params.type == cricket::DMT_CONTROL &&
1422 mediastream_signaling_->IsSctpSidAvailable(params.ssrc)) {
1423 // Received CONTROL on unused sid, process as an OPEN message.
1424 mediastream_signaling_->AddDataChannelFromOpenMessage(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001425 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001426 // otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001427}
1428
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001429// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001430bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001431 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1432 if (!bundle_enabled)
1433 return true;
1434
1435 const cricket::ContentGroup* bundle_group =
1436 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1437 ASSERT(bundle_group != NULL);
1438
1439 const cricket::ContentInfos& contents = desc->contents();
1440 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1441 citer != contents.end(); ++citer) {
1442 const cricket::ContentInfo* content = (&*citer);
1443 ASSERT(content != NULL);
1444 if (bundle_group->HasContentName(content->name) &&
1445 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1446 if (!HasRtcpMuxEnabled(content))
1447 return false;
1448 }
1449 }
1450 // RTCP-MUX is enabled in all the contents.
1451 return true;
1452}
1453
1454bool WebRtcSession::HasRtcpMuxEnabled(
1455 const cricket::ContentInfo* content) {
1456 const cricket::MediaContentDescription* description =
1457 static_cast<cricket::MediaContentDescription*>(content->description);
1458 return description->rtcp_mux();
1459}
1460
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001461bool WebRtcSession::ValidateSessionDescription(
1462 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001463 cricket::ContentSource source, std::string* err_desc) {
1464 std::string type;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001465 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001466 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001467 }
1468
1469 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001470 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001471 }
1472
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001473 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001474 Action action = GetAction(sdesc->type());
1475 if (source == cricket::CS_LOCAL) {
1476 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001477 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001478 } else {
1479 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001480 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001481 }
1482
1483 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001484 std::string crypto_error;
wu@webrtc.org364f2042013-11-20 21:49:41 +00001485 if (webrtc_session_desc_factory_->Secure() == cricket::SEC_REQUIRED &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001486 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001487 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001488 }
1489
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001490 // Verify ice-ufrag and ice-pwd.
1491 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001492 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001493 }
1494
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001495 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001496 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001497 }
1498
1499 // Verify m-lines in Answer when compared against Offer.
1500 if (action == kAnswer) {
1501 const cricket::SessionDescription* offer_desc =
1502 (source == cricket::CS_LOCAL) ? remote_description()->description() :
1503 local_description()->description();
1504 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001505 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001506 }
1507 }
1508
1509 return true;
1510}
1511
1512bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1513 return ((action == kOffer && state() == STATE_INIT) ||
1514 // update local offer
1515 (action == kOffer && state() == STATE_SENTINITIATE) ||
1516 // update the current ongoing session.
1517 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1518 (action == kOffer && state() == STATE_SENTACCEPT) ||
1519 (action == kOffer && state() == STATE_INPROGRESS) ||
1520 // accept remote offer
1521 (action == kAnswer && state() == STATE_RECEIVEDINITIATE) ||
1522 (action == kAnswer && state() == STATE_SENTPRACCEPT) ||
1523 (action == kPrAnswer && state() == STATE_RECEIVEDINITIATE) ||
1524 (action == kPrAnswer && state() == STATE_SENTPRACCEPT));
1525}
1526
1527bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1528 return ((action == kOffer && state() == STATE_INIT) ||
1529 // update remote offer
1530 (action == kOffer && state() == STATE_RECEIVEDINITIATE) ||
1531 // update the current ongoing session
1532 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1533 (action == kOffer && state() == STATE_SENTACCEPT) ||
1534 (action == kOffer && state() == STATE_INPROGRESS) ||
1535 // accept local offer
1536 (action == kAnswer && state() == STATE_SENTINITIATE) ||
1537 (action == kAnswer && state() == STATE_RECEIVEDPRACCEPT) ||
1538 (action == kPrAnswer && state() == STATE_SENTINITIATE) ||
1539 (action == kPrAnswer && state() == STATE_RECEIVEDPRACCEPT));
1540}
1541
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001542std::string WebRtcSession::GetSessionErrorMsg() {
1543 std::ostringstream desc;
1544 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1545 desc << kSessionErrorDesc << error_desc() << ".";
1546 return desc.str();
1547}
1548
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001549} // namespace webrtc