blob: ef6af49e5bf68f5741ca30fe6244f05dc97225cc [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
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000869void WebRtcSession::SetAudioPlayoutVolume(uint32 ssrc, double volume) {
870 ASSERT(signaling_thread()->IsCurrent());
871 ASSERT(volume >= 0 && volume <= 10);
872 if (!voice_channel_) {
873 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
874 return;
875 }
876
877 if (!voice_channel_->SetOutputScaling(ssrc, volume, volume))
878 ASSERT(false);
879}
880
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000881bool WebRtcSession::SetCaptureDevice(uint32 ssrc,
882 cricket::VideoCapturer* camera) {
883 ASSERT(signaling_thread()->IsCurrent());
884
885 if (!video_channel_.get()) {
886 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
887 // support video.
888 LOG(LS_WARNING) << "Video not used in this call.";
889 return false;
890 }
891 if (!video_channel_->SetCapturer(ssrc, camera)) {
892 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
893 // This in the normal case when the underlying media channel has already
894 // been deleted.
895 ASSERT(camera == NULL);
896 return false;
897 }
898 return true;
899}
900
901void WebRtcSession::SetVideoPlayout(uint32 ssrc,
902 bool enable,
903 cricket::VideoRenderer* renderer) {
904 ASSERT(signaling_thread()->IsCurrent());
905 if (!video_channel_) {
906 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
907 return;
908 }
909 if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) {
910 // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise.
911 // This in the normal case when the underlying media channel has already
912 // been deleted.
913 ASSERT(renderer == NULL);
914 }
915}
916
917void WebRtcSession::SetVideoSend(uint32 ssrc, bool enable,
918 const cricket::VideoOptions* options) {
919 ASSERT(signaling_thread()->IsCurrent());
920 if (!video_channel_) {
921 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
922 return;
923 }
924 if (!video_channel_->MuteStream(ssrc, !enable)) {
925 // Allow that MuteStream fail if |enable| is false but assert otherwise.
926 // This in the normal case when the underlying media channel has already
927 // been deleted.
928 ASSERT(enable == false);
929 return;
930 }
931 if (enable && options)
932 video_channel_->SetChannelOptions(*options);
933}
934
935bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
936 ASSERT(signaling_thread()->IsCurrent());
937 if (!voice_channel_) {
938 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
939 return false;
940 }
941 uint32 send_ssrc = 0;
942 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
943 // exists.
944 if (!GetAudioSsrcByTrackId(BaseSession::local_description(), track_id,
945 &send_ssrc)) {
946 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
947 return false;
948 }
949 return voice_channel_->CanInsertDtmf();
950}
951
952bool WebRtcSession::InsertDtmf(const std::string& track_id,
953 int code, int duration) {
954 ASSERT(signaling_thread()->IsCurrent());
955 if (!voice_channel_) {
956 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
957 return false;
958 }
959 uint32 send_ssrc = 0;
960 if (!VERIFY(GetAudioSsrcByTrackId(BaseSession::local_description(),
961 track_id, &send_ssrc))) {
962 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
963 return false;
964 }
965 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration,
966 cricket::DF_SEND)) {
967 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
968 return false;
969 }
970 return true;
971}
972
973sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
974 return &SignalVoiceChannelDestroyed;
975}
976
wu@webrtc.org78187522013-10-07 23:32:02 +0000977bool WebRtcSession::SendData(const cricket::SendDataParams& params,
978 const talk_base::Buffer& payload,
979 cricket::SendDataResult* result) {
980 if (!data_channel_.get()) {
981 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
982 return false;
983 }
984 return data_channel_->SendData(params, payload, result);
985}
986
987bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
988 if (!data_channel_.get()) {
989 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
990 return false;
991 }
wu@webrtc.org78187522013-10-07 23:32:02 +0000992 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
993 &DataChannel::OnChannelReady);
994 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
995 &DataChannel::OnDataReceived);
wu@webrtc.org78187522013-10-07 23:32:02 +0000996 return true;
997}
998
999void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001000 if (!data_channel_.get()) {
1001 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1002 return;
1003 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001004 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1005 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
1006}
1007
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001008void WebRtcSession::AddSctpDataStream(uint32 sid) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001009 if (!data_channel_.get()) {
1010 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1011 return;
1012 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001013 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1014 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001015}
1016
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001017void WebRtcSession::RemoveSctpDataStream(uint32 sid) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001018 if (!data_channel_.get()) {
1019 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1020 << "NULL.";
1021 return;
1022 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001023 data_channel_->RemoveRecvStream(sid);
1024 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001025}
1026
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001027bool WebRtcSession::ReadyToSendData() const {
1028 return data_channel_.get() && data_channel_->ready_to_send_data();
1029}
1030
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001031talk_base::scoped_refptr<DataChannel> WebRtcSession::CreateDataChannel(
wu@webrtc.org78187522013-10-07 23:32:02 +00001032 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001033 const InternalDataChannelInit* config) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001034 if (state() == STATE_RECEIVEDTERMINATE) {
1035 return NULL;
1036 }
1037 if (data_channel_type_ == cricket::DCT_NONE) {
1038 LOG(LS_ERROR) << "CreateDataChannel: Data is not supported in this call.";
1039 return NULL;
1040 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001041 InternalDataChannelInit new_config =
1042 config ? (*config) : InternalDataChannelInit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001043 if (data_channel_type_ == cricket::DCT_SCTP) {
1044 if (new_config.id < 0) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001045 talk_base::SSLRole role;
1046 if (GetSslRole(&role) &&
1047 !mediastream_signaling_->AllocateSctpSid(role, &new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001048 LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel.";
1049 return NULL;
1050 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001051 } else if (!mediastream_signaling_->IsSctpSidAvailable(new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001052 LOG(LS_ERROR) << "Failed to create a SCTP data channel "
1053 << "because the id is already in use or out of range.";
1054 return NULL;
1055 }
1056 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001057
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001058 talk_base::scoped_refptr<DataChannel> channel(DataChannel::Create(
1059 this, data_channel_type_, label, new_config));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001060 if (channel && !mediastream_signaling_->AddDataChannel(channel))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001061 return NULL;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001062
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001063 return channel;
1064}
1065
1066cricket::DataChannelType WebRtcSession::data_channel_type() const {
1067 return data_channel_type_;
1068}
1069
wu@webrtc.org91053e72013-08-10 07:18:04 +00001070bool WebRtcSession::IceRestartPending() const {
1071 return ice_restart_latch_->Get();
1072}
1073
1074void WebRtcSession::ResetIceRestartLatch() {
1075 ice_restart_latch_->Reset();
1076}
1077
1078void WebRtcSession::OnIdentityReady(talk_base::SSLIdentity* identity) {
1079 SetIdentity(identity);
1080}
1081
1082bool WebRtcSession::waiting_for_identity() const {
1083 return webrtc_session_desc_factory_->waiting_for_identity();
1084}
1085
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001086void WebRtcSession::SetIceConnectionState(
1087 PeerConnectionInterface::IceConnectionState state) {
1088 if (ice_connection_state_ == state) {
1089 return;
1090 }
1091
1092 // ASSERT that the requested transition is allowed. Note that
1093 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1094 // within PeerConnection). This switch statement should compile away when
1095 // ASSERTs are disabled.
1096 switch (ice_connection_state_) {
1097 case PeerConnectionInterface::kIceConnectionNew:
1098 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1099 break;
1100 case PeerConnectionInterface::kIceConnectionChecking:
1101 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1102 state == PeerConnectionInterface::kIceConnectionConnected);
1103 break;
1104 case PeerConnectionInterface::kIceConnectionConnected:
1105 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1106 state == PeerConnectionInterface::kIceConnectionChecking ||
1107 state == PeerConnectionInterface::kIceConnectionCompleted);
1108 break;
1109 case PeerConnectionInterface::kIceConnectionCompleted:
1110 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1111 state == PeerConnectionInterface::kIceConnectionDisconnected);
1112 break;
1113 case PeerConnectionInterface::kIceConnectionFailed:
1114 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1115 break;
1116 case PeerConnectionInterface::kIceConnectionDisconnected:
1117 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1118 state == PeerConnectionInterface::kIceConnectionConnected ||
1119 state == PeerConnectionInterface::kIceConnectionCompleted ||
1120 state == PeerConnectionInterface::kIceConnectionFailed);
1121 break;
1122 case PeerConnectionInterface::kIceConnectionClosed:
1123 ASSERT(false);
1124 break;
1125 default:
1126 ASSERT(false);
1127 break;
1128 }
1129
1130 ice_connection_state_ = state;
1131 if (ice_observer_) {
1132 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1133 }
1134}
1135
1136void WebRtcSession::OnTransportRequestSignaling(
1137 cricket::Transport* transport) {
1138 ASSERT(signaling_thread()->IsCurrent());
1139 transport->OnSignalingReady();
1140 if (ice_observer_) {
1141 ice_observer_->OnIceGatheringChange(
1142 PeerConnectionInterface::kIceGatheringGathering);
1143 }
1144}
1145
1146void WebRtcSession::OnTransportConnecting(cricket::Transport* transport) {
1147 ASSERT(signaling_thread()->IsCurrent());
1148 // start monitoring for the write state of the transport.
1149 OnTransportWritable(transport);
1150}
1151
1152void WebRtcSession::OnTransportWritable(cricket::Transport* transport) {
1153 ASSERT(signaling_thread()->IsCurrent());
1154 // TODO(bemasc): Expose more API from Transport to detect when
1155 // candidate selection starts or stops, due to success or failure.
1156 if (transport->all_channels_writable()) {
1157 if (ice_connection_state_ ==
1158 PeerConnectionInterface::kIceConnectionChecking ||
1159 ice_connection_state_ ==
1160 PeerConnectionInterface::kIceConnectionDisconnected) {
1161 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1162 }
1163 } else if (transport->HasChannels()) {
1164 // If the current state is Connected or Completed, then there were writable
1165 // channels but now there are not, so the next state must be Disconnected.
1166 if (ice_connection_state_ ==
1167 PeerConnectionInterface::kIceConnectionConnected ||
1168 ice_connection_state_ ==
1169 PeerConnectionInterface::kIceConnectionCompleted) {
1170 SetIceConnectionState(
1171 PeerConnectionInterface::kIceConnectionDisconnected);
1172 }
1173 }
1174}
1175
1176void WebRtcSession::OnTransportProxyCandidatesReady(
1177 cricket::TransportProxy* proxy, const cricket::Candidates& candidates) {
1178 ASSERT(signaling_thread()->IsCurrent());
1179 ProcessNewLocalCandidate(proxy->content_name(), candidates);
1180}
1181
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001182void WebRtcSession::OnCandidatesAllocationDone() {
1183 ASSERT(signaling_thread()->IsCurrent());
1184 if (ice_observer_) {
1185 ice_observer_->OnIceGatheringChange(
1186 PeerConnectionInterface::kIceGatheringComplete);
1187 ice_observer_->OnIceComplete();
1188 }
1189}
1190
1191// Enabling voice and video channel.
1192void WebRtcSession::EnableChannels() {
1193 if (voice_channel_ && !voice_channel_->enabled())
1194 voice_channel_->Enable(true);
1195
1196 if (video_channel_ && !video_channel_->enabled())
1197 video_channel_->Enable(true);
1198
1199 if (data_channel_.get() && !data_channel_->enabled())
1200 data_channel_->Enable(true);
1201}
1202
1203void WebRtcSession::ProcessNewLocalCandidate(
1204 const std::string& content_name,
1205 const cricket::Candidates& candidates) {
1206 int sdp_mline_index;
1207 if (!GetLocalCandidateMediaIndex(content_name, &sdp_mline_index)) {
1208 LOG(LS_ERROR) << "ProcessNewLocalCandidate: content name "
1209 << content_name << " not found";
1210 return;
1211 }
1212
1213 for (cricket::Candidates::const_iterator citer = candidates.begin();
1214 citer != candidates.end(); ++citer) {
1215 // Use content_name as the candidate media id.
1216 JsepIceCandidate candidate(content_name, sdp_mline_index, *citer);
1217 if (ice_observer_) {
1218 ice_observer_->OnIceCandidate(&candidate);
1219 }
1220 if (local_desc_) {
1221 local_desc_->AddCandidate(&candidate);
1222 }
1223 }
1224}
1225
1226// Returns the media index for a local ice candidate given the content name.
1227bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1228 int* sdp_mline_index) {
1229 if (!BaseSession::local_description() || !sdp_mline_index)
1230 return false;
1231
1232 bool content_found = false;
1233 const ContentInfos& contents = BaseSession::local_description()->contents();
1234 for (size_t index = 0; index < contents.size(); ++index) {
1235 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001236 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001237 content_found = true;
1238 break;
1239 }
1240 }
1241 return content_found;
1242}
1243
1244bool WebRtcSession::UseCandidatesInSessionDescription(
1245 const SessionDescriptionInterface* remote_desc) {
1246 if (!remote_desc)
1247 return true;
1248 bool ret = true;
1249 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1250 const IceCandidateCollection* candidates = remote_desc->candidates(m);
1251 for (size_t n = 0; n < candidates->count(); ++n) {
1252 ret = UseCandidate(candidates->at(n));
1253 if (!ret)
1254 break;
1255 }
1256 }
1257 return ret;
1258}
1259
1260bool WebRtcSession::UseCandidate(
1261 const IceCandidateInterface* candidate) {
1262
1263 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
1264 size_t remote_content_size =
1265 BaseSession::remote_description()->contents().size();
1266 if (mediacontent_index >= remote_content_size) {
1267 LOG(LS_ERROR)
1268 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1269 return false;
1270 }
1271
1272 cricket::ContentInfo content =
1273 BaseSession::remote_description()->contents()[mediacontent_index];
1274 std::vector<cricket::Candidate> candidates;
1275 candidates.push_back(candidate->candidate());
1276 // Invoking BaseSession method to handle remote candidates.
1277 std::string error;
1278 if (OnRemoteCandidates(content.name, candidates, &error)) {
1279 // Candidates successfully submitted for checking.
1280 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1281 ice_connection_state_ ==
1282 PeerConnectionInterface::kIceConnectionDisconnected) {
1283 // If state is New, then the session has just gotten its first remote ICE
1284 // candidates, so go to Checking.
1285 // If state is Disconnected, the session is re-using old candidates or
1286 // receiving additional ones, so go to Checking.
1287 // If state is Connected, stay Connected.
1288 // TODO(bemasc): If state is Connected, and the new candidates are for a
1289 // newly added transport, then the state actually _should_ move to
1290 // checking. Add a way to distinguish that case.
1291 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1292 }
1293 // TODO(bemasc): If state is Completed, go back to Connected.
1294 } else {
1295 LOG(LS_WARNING) << error;
1296 }
1297 return true;
1298}
1299
1300void WebRtcSession::RemoveUnusedChannelsAndTransports(
1301 const SessionDescription* desc) {
1302 const cricket::ContentInfo* voice_info =
1303 cricket::GetFirstAudioContent(desc);
1304 if ((!voice_info || voice_info->rejected) && voice_channel_) {
1305 mediastream_signaling_->OnAudioChannelClose();
1306 SignalVoiceChannelDestroyed();
1307 const std::string content_name = voice_channel_->content_name();
1308 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
1309 DestroyTransportProxy(content_name);
1310 }
1311
1312 const cricket::ContentInfo* video_info =
1313 cricket::GetFirstVideoContent(desc);
1314 if ((!video_info || video_info->rejected) && video_channel_) {
1315 mediastream_signaling_->OnVideoChannelClose();
1316 SignalVideoChannelDestroyed();
1317 const std::string content_name = video_channel_->content_name();
1318 channel_manager_->DestroyVideoChannel(video_channel_.release());
1319 DestroyTransportProxy(content_name);
1320 }
1321
1322 const cricket::ContentInfo* data_info =
1323 cricket::GetFirstDataContent(desc);
1324 if ((!data_info || data_info->rejected) && data_channel_) {
1325 mediastream_signaling_->OnDataChannelClose();
1326 SignalDataChannelDestroyed();
1327 const std::string content_name = data_channel_->content_name();
1328 channel_manager_->DestroyDataChannel(data_channel_.release());
1329 DestroyTransportProxy(content_name);
1330 }
1331}
1332
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001333// TODO(mallinath) - Add a correct error code if the channels are not creatued
1334// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001335bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
1336 // Disabling the BUNDLE flag in PortAllocator if offer disabled it.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001337 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1338 if (state() == STATE_INIT && !bundle_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001339 port_allocator()->set_flags(port_allocator()->flags() &
1340 ~cricket::PORTALLOCATOR_ENABLE_BUNDLE);
1341 }
1342
1343 // Creating the media channels and transport proxies.
1344 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1345 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001346 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001347 LOG(LS_ERROR) << "Failed to create voice channel.";
1348 return false;
1349 }
1350 }
1351
1352 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1353 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001354 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001355 LOG(LS_ERROR) << "Failed to create video channel.";
1356 return false;
1357 }
1358 }
1359
1360 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1361 if (data_channel_type_ != cricket::DCT_NONE &&
1362 data && !data->rejected && !data_channel_.get()) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001363 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001364 LOG(LS_ERROR) << "Failed to create data channel.";
1365 return false;
1366 }
1367 }
1368
1369 return true;
1370}
1371
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001372bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001373 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001374 this, content->name, true));
wu@webrtc.orgde305012013-10-31 15:40:38 +00001375 if (!voice_channel_.get())
1376 return false;
1377
1378 if (dscp_enabled_) {
1379 cricket::AudioOptions options;
1380 options.dscp.Set(true);
1381 voice_channel_->SetChannelOptions(options);
1382 }
1383 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001384}
1385
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001386bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001387 video_channel_.reset(channel_manager_->CreateVideoChannel(
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001388 this, content->name, true, voice_channel_.get()));
wu@webrtc.orgde305012013-10-31 15:40:38 +00001389 if (!video_channel_.get())
1390 return false;
1391
1392 if (dscp_enabled_) {
1393 cricket::VideoOptions options;
1394 options.dscp.Set(true);
1395 video_channel_->SetChannelOptions(options);
1396 }
1397 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001398}
1399
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001400bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001401 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001402 data_channel_.reset(channel_manager_->CreateDataChannel(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001403 this, content->name, !sctp, data_channel_type_));
wu@webrtc.org91053e72013-08-10 07:18:04 +00001404 if (!data_channel_.get()) {
1405 return false;
1406 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001407 if (sctp) {
1408 mediastream_signaling_->OnDataTransportCreatedForSctp();
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001409 data_channel_->SignalDataReceived.connect(
1410 this, &WebRtcSession::OnDataChannelMessageReceived);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001411 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001412 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001413}
1414
1415void WebRtcSession::CopySavedCandidates(
1416 SessionDescriptionInterface* dest_desc) {
1417 if (!dest_desc) {
1418 ASSERT(false);
1419 return;
1420 }
1421 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
1422 dest_desc->AddCandidate(saved_candidates_[i]);
1423 delete saved_candidates_[i];
1424 }
1425 saved_candidates_.clear();
1426}
1427
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001428void WebRtcSession::OnDataChannelMessageReceived(
1429 cricket::DataChannel* channel,
1430 const cricket::ReceiveDataParams& params,
1431 const talk_base::Buffer& payload) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001432 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001433 if (params.type == cricket::DMT_CONTROL &&
1434 mediastream_signaling_->IsSctpSidAvailable(params.ssrc)) {
1435 // Received CONTROL on unused sid, process as an OPEN message.
1436 mediastream_signaling_->AddDataChannelFromOpenMessage(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001437 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001438 // otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001439}
1440
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001441// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001442bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001443 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1444 if (!bundle_enabled)
1445 return true;
1446
1447 const cricket::ContentGroup* bundle_group =
1448 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1449 ASSERT(bundle_group != NULL);
1450
1451 const cricket::ContentInfos& contents = desc->contents();
1452 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1453 citer != contents.end(); ++citer) {
1454 const cricket::ContentInfo* content = (&*citer);
1455 ASSERT(content != NULL);
1456 if (bundle_group->HasContentName(content->name) &&
1457 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1458 if (!HasRtcpMuxEnabled(content))
1459 return false;
1460 }
1461 }
1462 // RTCP-MUX is enabled in all the contents.
1463 return true;
1464}
1465
1466bool WebRtcSession::HasRtcpMuxEnabled(
1467 const cricket::ContentInfo* content) {
1468 const cricket::MediaContentDescription* description =
1469 static_cast<cricket::MediaContentDescription*>(content->description);
1470 return description->rtcp_mux();
1471}
1472
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001473bool WebRtcSession::ValidateSessionDescription(
1474 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001475 cricket::ContentSource source, std::string* err_desc) {
1476 std::string type;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001477 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001478 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001479 }
1480
1481 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001482 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001483 }
1484
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001485 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001486 Action action = GetAction(sdesc->type());
1487 if (source == cricket::CS_LOCAL) {
1488 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001489 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001490 } else {
1491 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001492 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001493 }
1494
1495 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001496 std::string crypto_error;
wu@webrtc.org364f2042013-11-20 21:49:41 +00001497 if (webrtc_session_desc_factory_->Secure() == cricket::SEC_REQUIRED &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001498 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001499 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001500 }
1501
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001502 // Verify ice-ufrag and ice-pwd.
1503 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001504 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001505 }
1506
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001507 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001508 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001509 }
1510
1511 // Verify m-lines in Answer when compared against Offer.
1512 if (action == kAnswer) {
1513 const cricket::SessionDescription* offer_desc =
1514 (source == cricket::CS_LOCAL) ? remote_description()->description() :
1515 local_description()->description();
1516 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001517 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001518 }
1519 }
1520
1521 return true;
1522}
1523
1524bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1525 return ((action == kOffer && state() == STATE_INIT) ||
1526 // update local offer
1527 (action == kOffer && state() == STATE_SENTINITIATE) ||
1528 // update the current ongoing session.
1529 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1530 (action == kOffer && state() == STATE_SENTACCEPT) ||
1531 (action == kOffer && state() == STATE_INPROGRESS) ||
1532 // accept remote offer
1533 (action == kAnswer && state() == STATE_RECEIVEDINITIATE) ||
1534 (action == kAnswer && state() == STATE_SENTPRACCEPT) ||
1535 (action == kPrAnswer && state() == STATE_RECEIVEDINITIATE) ||
1536 (action == kPrAnswer && state() == STATE_SENTPRACCEPT));
1537}
1538
1539bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1540 return ((action == kOffer && state() == STATE_INIT) ||
1541 // update remote offer
1542 (action == kOffer && state() == STATE_RECEIVEDINITIATE) ||
1543 // update the current ongoing session
1544 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1545 (action == kOffer && state() == STATE_SENTACCEPT) ||
1546 (action == kOffer && state() == STATE_INPROGRESS) ||
1547 // accept local offer
1548 (action == kAnswer && state() == STATE_SENTINITIATE) ||
1549 (action == kAnswer && state() == STATE_RECEIVEDPRACCEPT) ||
1550 (action == kPrAnswer && state() == STATE_SENTINITIATE) ||
1551 (action == kPrAnswer && state() == STATE_RECEIVEDPRACCEPT));
1552}
1553
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001554std::string WebRtcSession::GetSessionErrorMsg() {
1555 std::ostringstream desc;
1556 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1557 desc << kSessionErrorDesc << error_desc() << ".";
1558 return desc.str();
1559}
1560
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001561} // namespace webrtc