blob: f04baeaf598e777b6f6b2d941712f2a43cf2c41b [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2012, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/app/webrtc/webrtcsession.h"
29
pbos@webrtc.org371243d2014-03-07 15:22:04 +000030#include <limits.h>
31
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032#include <algorithm>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033#include <vector>
34
35#include "talk/app/webrtc/jsepicecandidate.h"
36#include "talk/app/webrtc/jsepsessiondescription.h"
37#include "talk/app/webrtc/mediaconstraintsinterface.h"
38#include "talk/app/webrtc/mediastreamsignaling.h"
39#include "talk/app/webrtc/peerconnectioninterface.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000040#include "talk/app/webrtc/webrtcsessiondescriptionfactory.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041#include "talk/base/helpers.h"
42#include "talk/base/logging.h"
43#include "talk/base/stringencode.h"
44#include "talk/media/base/constants.h"
45#include "talk/media/base/videocapturer.h"
46#include "talk/session/media/channel.h"
47#include "talk/session/media/channelmanager.h"
48#include "talk/session/media/mediasession.h"
49
50using cricket::ContentInfo;
51using cricket::ContentInfos;
52using cricket::MediaContentDescription;
53using cricket::SessionDescription;
54using cricket::TransportInfo;
55
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056namespace webrtc {
57
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000059const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
60 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000061const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062const char kInvalidCandidates[] = "Description contains invalid candidates.";
63const char kInvalidSdp[] = "Invalid session description.";
64const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000065 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
66const char kPushDownTDFailed[] =
67 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000068const char kSdpWithoutDtlsFingerprint[] =
69 "Called with SDP without DTLS fingerprint.";
70const char kSdpWithoutSdesCrypto[] =
71 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000072const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000073 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000075const char kSessionErrorDesc[] = "Session error description: ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076
77// Compares |answer| against |offer|. Comparision is done
78// for number of m-lines in answer against offer. If matches true will be
79// returned otherwise false.
80static bool VerifyMediaDescriptions(
81 const SessionDescription* answer, const SessionDescription* offer) {
82 if (offer->contents().size() != answer->contents().size())
83 return false;
84
85 for (size_t i = 0; i < offer->contents().size(); ++i) {
86 if ((offer->contents()[i].name) != answer->contents()[i].name) {
87 return false;
88 }
89 }
90 return true;
91}
92
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093// Checks that each non-rejected content has SDES crypto keys or a DTLS
94// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
95// keys, will be caught in Transport negotiation, and backstopped by Channel's
96// |secure_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000097static bool VerifyCrypto(const SessionDescription* desc,
98 bool dtls_enabled,
99 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100 const ContentInfos& contents = desc->contents();
101 for (size_t index = 0; index < contents.size(); ++index) {
102 const ContentInfo* cinfo = &contents[index];
103 if (cinfo->rejected) {
104 continue;
105 }
106
107 // If the content isn't rejected, crypto must be present.
108 const MediaContentDescription* media =
109 static_cast<const MediaContentDescription*>(cinfo->description);
110 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
111 if (!media || !tinfo) {
112 // Something is not right.
113 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000114 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 return false;
116 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000117 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000118 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000119 LOG(LS_WARNING) <<
120 "Session description must have DTLS fingerprint if DTLS enabled.";
121 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000122 return false;
123 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000124 } else {
125 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000126 LOG(LS_WARNING) <<
127 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000128 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000129 return false;
130 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 }
132 }
133
134 return true;
135}
136
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000137// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
138static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
139 const ContentInfos& contents = desc->contents();
140 for (size_t index = 0; index < contents.size(); ++index) {
141 const ContentInfo* cinfo = &contents[index];
142 if (cinfo->rejected) {
143 continue;
144 }
145
146 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
147 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
148 if (!tinfo) {
149 // Something is not right.
150 LOG(LS_ERROR) << kInvalidSdp;
151 return false;
152 }
153 if (tinfo->description.ice_ufrag.empty() ||
154 tinfo->description.ice_pwd.empty()) {
155 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
156 return false;
157 }
158 }
159 return true;
160}
161
wu@webrtc.org91053e72013-08-10 07:18:04 +0000162// Forces |sdesc->crypto_required| to the appropriate state based on the
163// current security policy, to ensure a failure occurs if there is an error
164// in crypto negotiation.
165// Called when processing the local session description.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000166static void UpdateSessionDescriptionSecurePolicy(cricket::CryptoType type,
167 SessionDescription* sdesc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000168 if (!sdesc) {
169 return;
170 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171
wu@webrtc.org91053e72013-08-10 07:18:04 +0000172 // Updating the |crypto_required_| in MediaContentDescription to the
173 // appropriate state based on the current security policy.
174 for (cricket::ContentInfos::iterator iter = sdesc->contents().begin();
175 iter != sdesc->contents().end(); ++iter) {
176 if (cricket::IsMediaContent(&*iter)) {
177 MediaContentDescription* mdesc =
178 static_cast<MediaContentDescription*> (iter->description);
179 if (mdesc) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000180 mdesc->set_crypto_required(type);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000181 }
182 }
183 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184}
185
186static bool GetAudioSsrcByTrackId(
187 const SessionDescription* session_description,
188 const std::string& track_id, uint32 *ssrc) {
189 const cricket::ContentInfo* audio_info =
190 cricket::GetFirstAudioContent(session_description);
191 if (!audio_info) {
192 LOG(LS_ERROR) << "Audio not used in this call";
193 return false;
194 }
195
196 const cricket::MediaContentDescription* audio_content =
197 static_cast<const cricket::MediaContentDescription*>(
198 audio_info->description);
199 cricket::StreamParams stream;
200 if (!cricket::GetStreamByIds(audio_content->streams(), "", track_id,
201 &stream)) {
202 return false;
203 }
204 *ssrc = stream.first_ssrc();
205 return true;
206}
207
208static bool GetTrackIdBySsrc(const SessionDescription* session_description,
209 uint32 ssrc, std::string* track_id) {
210 ASSERT(track_id != NULL);
211
212 cricket::StreamParams stream_out;
213 const cricket::ContentInfo* audio_info =
214 cricket::GetFirstAudioContent(session_description);
215 if (!audio_info) {
216 return false;
217 }
218 const cricket::MediaContentDescription* audio_content =
219 static_cast<const cricket::MediaContentDescription*>(
220 audio_info->description);
221
222 if (cricket::GetStreamBySsrc(audio_content->streams(), ssrc, &stream_out)) {
223 *track_id = stream_out.id;
224 return true;
225 }
226
227 const cricket::ContentInfo* video_info =
228 cricket::GetFirstVideoContent(session_description);
229 if (!video_info) {
230 return false;
231 }
232 const cricket::MediaContentDescription* video_content =
233 static_cast<const cricket::MediaContentDescription*>(
234 video_info->description);
235
236 if (cricket::GetStreamBySsrc(video_content->streams(), ssrc, &stream_out)) {
237 *track_id = stream_out.id;
238 return true;
239 }
240 return false;
241}
242
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000243static bool BadSdp(const std::string& source,
244 const std::string& type,
245 const std::string& reason,
246 std::string* err_desc) {
247 std::ostringstream desc;
248 desc << "Failed to set " << source << " " << type << " sdp: " << reason;
249
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000251 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000253 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254 return false;
255}
256
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000258 const std::string& type,
259 const std::string& reason,
260 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000262 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000264 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265 }
266}
267
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000268static bool BadLocalSdp(const std::string& type,
269 const std::string& reason,
270 std::string* err_desc) {
271 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
272}
273
274static bool BadRemoteSdp(const std::string& type,
275 const std::string& reason,
276 std::string* err_desc) {
277 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
278}
279
280static bool BadOfferSdp(cricket::ContentSource source,
281 const std::string& reason,
282 std::string* err_desc) {
283 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
284}
285
286static bool BadPranswerSdp(cricket::ContentSource source,
287 const std::string& reason,
288 std::string* err_desc) {
289 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
290 reason, err_desc);
291}
292
293static bool BadAnswerSdp(cricket::ContentSource source,
294 const std::string& reason,
295 std::string* err_desc) {
296 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297}
298
299#define GET_STRING_OF_STATE(state) \
300 case cricket::BaseSession::state: \
301 result = #state; \
302 break;
303
304static std::string GetStateString(cricket::BaseSession::State state) {
305 std::string result;
306 switch (state) {
307 GET_STRING_OF_STATE(STATE_INIT)
308 GET_STRING_OF_STATE(STATE_SENTINITIATE)
309 GET_STRING_OF_STATE(STATE_RECEIVEDINITIATE)
310 GET_STRING_OF_STATE(STATE_SENTPRACCEPT)
311 GET_STRING_OF_STATE(STATE_SENTACCEPT)
312 GET_STRING_OF_STATE(STATE_RECEIVEDPRACCEPT)
313 GET_STRING_OF_STATE(STATE_RECEIVEDACCEPT)
314 GET_STRING_OF_STATE(STATE_SENTMODIFY)
315 GET_STRING_OF_STATE(STATE_RECEIVEDMODIFY)
316 GET_STRING_OF_STATE(STATE_SENTREJECT)
317 GET_STRING_OF_STATE(STATE_RECEIVEDREJECT)
318 GET_STRING_OF_STATE(STATE_SENTREDIRECT)
319 GET_STRING_OF_STATE(STATE_SENTTERMINATE)
320 GET_STRING_OF_STATE(STATE_RECEIVEDTERMINATE)
321 GET_STRING_OF_STATE(STATE_INPROGRESS)
322 GET_STRING_OF_STATE(STATE_DEINIT)
323 default:
324 ASSERT(false);
325 break;
326 }
327 return result;
328}
329
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000330#define GET_STRING_OF_ERROR_CODE(err) \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331 case cricket::BaseSession::err: \
332 result = #err; \
333 break;
334
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000335static std::string GetErrorCodeString(cricket::BaseSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336 std::string result;
337 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000338 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
339 GET_STRING_OF_ERROR_CODE(ERROR_TIME)
340 GET_STRING_OF_ERROR_CODE(ERROR_RESPONSE)
341 GET_STRING_OF_ERROR_CODE(ERROR_NETWORK)
342 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
343 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344 default:
345 ASSERT(false);
346 break;
347 }
348 return result;
349}
350
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000351static std::string MakeErrorString(const std::string& error,
352 const std::string& desc) {
353 std::ostringstream ret;
354 ret << error << " " << desc;
355 return ret.str();
356}
357
358static std::string MakeTdErrorString(const std::string& desc) {
359 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000360}
361
362// Help class used to remember if a a remote peer has requested ice restart by
363// by sending a description with new ice ufrag and password.
364class IceRestartAnswerLatch {
365 public:
366 IceRestartAnswerLatch() : ice_restart_(false) { }
367
wu@webrtc.org91053e72013-08-10 07:18:04 +0000368 // Returns true if CheckForRemoteIceRestart has been called with a new session
369 // description where ice password and ufrag has changed since last time
370 // Reset() was called.
371 bool Get() const {
372 return ice_restart_;
373 }
374
375 void Reset() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376 if (ice_restart_) {
377 ice_restart_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000378 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379 }
380
381 void CheckForRemoteIceRestart(
382 const SessionDescriptionInterface* old_desc,
383 const SessionDescriptionInterface* new_desc) {
384 if (!old_desc || new_desc->type() != SessionDescriptionInterface::kOffer) {
385 return;
386 }
387 const SessionDescription* new_sd = new_desc->description();
388 const SessionDescription* old_sd = old_desc->description();
389 const ContentInfos& contents = new_sd->contents();
390 for (size_t index = 0; index < contents.size(); ++index) {
391 const ContentInfo* cinfo = &contents[index];
392 if (cinfo->rejected) {
393 continue;
394 }
395 // If the content isn't rejected, check if ufrag and password has
396 // changed.
397 const cricket::TransportDescription* new_transport_desc =
398 new_sd->GetTransportDescriptionByName(cinfo->name);
399 const cricket::TransportDescription* old_transport_desc =
400 old_sd->GetTransportDescriptionByName(cinfo->name);
401 if (!new_transport_desc || !old_transport_desc) {
402 // No transport description exist. This is not an ice restart.
403 continue;
404 }
405 if (new_transport_desc->ice_pwd != old_transport_desc->ice_pwd &&
406 new_transport_desc->ice_ufrag != old_transport_desc->ice_ufrag) {
407 LOG(LS_INFO) << "Remote peer request ice restart.";
408 ice_restart_ = true;
409 break;
410 }
411 }
412 }
413
414 private:
415 bool ice_restart_;
416};
417
wu@webrtc.org91053e72013-08-10 07:18:04 +0000418WebRtcSession::WebRtcSession(
419 cricket::ChannelManager* channel_manager,
420 talk_base::Thread* signaling_thread,
421 talk_base::Thread* worker_thread,
422 cricket::PortAllocator* port_allocator,
423 MediaStreamSignaling* mediastream_signaling)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000424 : cricket::BaseSession(signaling_thread, worker_thread, port_allocator,
425 talk_base::ToString(talk_base::CreateRandomId64() &
426 LLONG_MAX),
427 cricket::NS_JINGLE_RTP, false),
428 // RFC 3264: The numeric value of the session id and version in the
429 // o line MUST be representable with a "64 bit signed integer".
430 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
431 channel_manager_(channel_manager),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000432 mediastream_signaling_(mediastream_signaling),
433 ice_observer_(NULL),
434 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000435 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000436 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000437 data_channel_type_(cricket::DCT_NONE),
438 ice_restart_latch_(new IceRestartAnswerLatch) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439}
440
441WebRtcSession::~WebRtcSession() {
442 if (voice_channel_.get()) {
443 SignalVoiceChannelDestroyed();
444 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
445 }
446 if (video_channel_.get()) {
447 SignalVideoChannelDestroyed();
448 channel_manager_->DestroyVideoChannel(video_channel_.release());
449 }
450 if (data_channel_.get()) {
451 SignalDataChannelDestroyed();
452 channel_manager_->DestroyDataChannel(data_channel_.release());
453 }
454 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
455 delete saved_candidates_[i];
456 }
457 delete identity();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458}
459
wu@webrtc.org91053e72013-08-10 07:18:04 +0000460bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000461 const PeerConnectionFactoryInterface::Options& options,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000462 const MediaConstraintsInterface* constraints,
463 DTLSIdentityServiceInterface* dtls_identity_service) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464 // TODO(perkj): Take |constraints| into consideration. Return false if not all
465 // mandatory constraints can be fulfilled. Note that |constraints|
466 // can be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000467 bool value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000468
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000469 if (options.disable_encryption) {
470 dtls_enabled_ = false;
471 } else {
472 // Enable DTLS by default if |dtls_identity_service| is valid.
473 dtls_enabled_ = (dtls_identity_service != NULL);
474 // |constraints| can override the default |dtls_enabled_| value.
475 if (FindConstraint(
476 constraints,
477 MediaConstraintsInterface::kEnableDtlsSrtp,
478 &value, NULL)) {
479 dtls_enabled_ = value;
480 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000481 }
482
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000484 // It takes precendence over the disable_sctp_data_channels
485 // PeerConnectionFactoryInterface::Options.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486 if (FindConstraint(
487 constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
488 &value, NULL) && value) {
489 LOG(LS_INFO) << "Allowing RTP data engine.";
490 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000491 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000492 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000493 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000494 LOG(LS_INFO) << "Allowing SCTP data engine.";
495 data_channel_type_ = cricket::DCT_SCTP;
496 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 }
498 if (data_channel_type_ != cricket::DCT_NONE) {
499 mediastream_signaling_->SetDataChannelFactory(this);
500 }
501
wu@webrtc.orgde305012013-10-31 15:40:38 +0000502 // Find DSCP constraint.
503 if (FindConstraint(
504 constraints,
505 MediaConstraintsInterface::kEnableDscp,
506 &value, NULL)) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000507 audio_options_.dscp.Set(value);
508 video_options_.dscp.Set(value);
509 }
510
511 // Find Suspend Below Min Bitrate constraint.
512 if (FindConstraint(
513 constraints,
514 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
515 &value,
516 NULL)) {
517 video_options_.suspend_below_min_bitrate.Set(value);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000518 }
519
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520 const cricket::VideoCodec default_codec(
521 JsepSessionDescription::kDefaultVideoCodecId,
522 JsepSessionDescription::kDefaultVideoCodecName,
523 JsepSessionDescription::kMaxVideoCodecWidth,
524 JsepSessionDescription::kMaxVideoCodecHeight,
525 JsepSessionDescription::kDefaultVideoCodecFramerate,
526 JsepSessionDescription::kDefaultVideoCodecPreference);
527 channel_manager_->SetDefaultVideoEncoderConfig(
528 cricket::VideoEncoderConfig(default_codec));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000529
530 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
531 signaling_thread(),
532 channel_manager_,
533 mediastream_signaling_,
534 dtls_identity_service,
535 this,
536 id(),
537 data_channel_type_,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000538 dtls_enabled_));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000539
540 webrtc_session_desc_factory_->SignalIdentityReady.connect(
541 this, &WebRtcSession::OnIdentityReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000542
wu@webrtc.org97077a32013-10-25 21:18:33 +0000543 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000544 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000545 }
546
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547 return true;
548}
549
550void WebRtcSession::Terminate() {
551 SetState(STATE_RECEIVEDTERMINATE);
552 RemoveUnusedChannelsAndTransports(NULL);
553 ASSERT(voice_channel_.get() == NULL);
554 ASSERT(video_channel_.get() == NULL);
555 ASSERT(data_channel_.get() == NULL);
556}
557
558bool WebRtcSession::StartCandidatesAllocation() {
559 // SpeculativelyConnectTransportChannels, will call ConnectChannels method
560 // from TransportProxy to start gathering ice candidates.
561 SpeculativelyConnectAllTransportChannels();
562 if (!saved_candidates_.empty()) {
563 // If there are saved candidates which arrived before local description is
564 // set, copy those to remote description.
565 CopySavedCandidates(remote_desc_.get());
566 }
567 // Push remote candidates present in remote description to transport channels.
568 UseCandidatesInSessionDescription(remote_desc_.get());
569 return true;
570}
571
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000572void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
573 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574}
575
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000576cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
577 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000578}
579
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000580bool WebRtcSession::GetSslRole(talk_base::SSLRole* role) {
581 if (local_description() == NULL || remote_description() == NULL) {
582 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
583 << "SSL Role of the session.";
584 return false;
585 }
586
587 // TODO(mallinath) - Return role of each transport, as role may differ from
588 // one another.
589 // In current implementaion we just return the role of first transport in the
590 // transport map.
591 for (cricket::TransportMap::const_iterator iter = transport_proxies().begin();
592 iter != transport_proxies().end(); ++iter) {
593 if (iter->second->impl()) {
594 return iter->second->impl()->GetSslRole(role);
595 }
596 }
597 return false;
598}
599
wu@webrtc.org91053e72013-08-10 07:18:04 +0000600void WebRtcSession::CreateOffer(CreateSessionDescriptionObserver* observer,
601 const MediaConstraintsInterface* constraints) {
602 webrtc_session_desc_factory_->CreateOffer(observer, constraints);
603}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604
wu@webrtc.org91053e72013-08-10 07:18:04 +0000605void WebRtcSession::CreateAnswer(CreateSessionDescriptionObserver* observer,
606 const MediaConstraintsInterface* constraints) {
607 webrtc_session_desc_factory_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608}
609
610bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
611 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000612 // Takes the ownership of |desc| regardless of the result.
613 talk_base::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
614
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000615 // Validate SDP.
616 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
617 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 }
619
620 // Update the initiator flag if this session is the initiator.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000621 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622 if (state() == STATE_INIT && action == kOffer) {
623 set_initiator(true);
624 }
625
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000626 cricket::SecurePolicy sdes_policy =
627 webrtc_session_desc_factory_->SdesPolicy();
628 cricket::CryptoType crypto_required = dtls_enabled_ ?
629 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
630 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000632 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633
634 set_local_description(desc->description()->Copy());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000635 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636
637 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000638 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 // TODO(mallinath) - Handle CreateChannel failure, as new local description
640 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000641 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642 }
643
644 // Remove channel and transport proxies, if MediaContentDescription is
645 // rejected.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000646 RemoveUnusedChannelsAndTransports(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000648 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649 return false;
650 }
651 // Kick starting the ice candidates allocation.
652 StartCandidatesAllocation();
653
654 // Update state and SSRC of local MediaStreams and DataChannels based on the
655 // local session description.
656 mediastream_signaling_->OnLocalDescriptionChanged(local_desc_.get());
657
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000658 talk_base::SSLRole role;
659 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
660 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
661 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000662 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000663 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000664 }
665 return true;
666}
667
668bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
669 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000670 // Takes the ownership of |desc| regardless of the result.
671 talk_base::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
672
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000673 // Validate SDP.
674 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
675 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 }
677
678 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000679 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000680 if (action == kOffer && !CreateChannels(desc->description())) {
681 // TODO(mallinath) - Handle CreateChannel failure, as new local description
682 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000683 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000684 }
685
686 // Remove channel and transport proxies, if MediaContentDescription is
687 // rejected.
688 RemoveUnusedChannelsAndTransports(desc->description());
689
690 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
691 // is called.
692 set_remote_description(desc->description()->Copy());
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000693 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694 return false;
695 }
696
697 // Update remote MediaStreams.
698 mediastream_signaling_->OnRemoteDescriptionChanged(desc);
699 if (local_description() && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000700 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000701 }
702
703 // Copy all saved candidates.
704 CopySavedCandidates(desc);
705 // We retain all received candidates.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000706 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
707 remote_desc_.get(), desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 // Check if this new SessionDescription contains new ice ufrag and password
709 // that indicates the remote peer requests ice restart.
710 ice_restart_latch_->CheckForRemoteIceRestart(remote_desc_.get(),
711 desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000712 remote_desc_.reset(desc_temp.release());
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000713
714 talk_base::SSLRole role;
715 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
716 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
717 }
718
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000720 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721 }
722 return true;
723}
724
725bool WebRtcSession::UpdateSessionState(
726 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727 std::string* err_desc) {
728 // If there's already a pending error then no state transition should happen.
729 // But all call-sites should be verifying this before calling us!
730 ASSERT(error() == cricket::BaseSession::ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000731 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000732 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000733 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
734 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 }
736 SetState(source == cricket::CS_LOCAL ?
737 STATE_SENTINITIATE : STATE_RECEIVEDINITIATE);
738 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000739 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000740 }
741 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000742 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
743 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 }
745 EnableChannels();
746 SetState(source == cricket::CS_LOCAL ?
747 STATE_SENTPRACCEPT : STATE_RECEIVEDPRACCEPT);
748 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000749 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750 }
751 } else if (action == kAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000752 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
753 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000754 }
755 MaybeEnableMuxingSupport();
756 EnableChannels();
757 SetState(source == cricket::CS_LOCAL ?
758 STATE_SENTACCEPT : STATE_RECEIVEDACCEPT);
759 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000760 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761 }
762 }
763 return true;
764}
765
766WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
767 if (type == SessionDescriptionInterface::kOffer) {
768 return WebRtcSession::kOffer;
769 } else if (type == SessionDescriptionInterface::kPrAnswer) {
770 return WebRtcSession::kPrAnswer;
771 } else if (type == SessionDescriptionInterface::kAnswer) {
772 return WebRtcSession::kAnswer;
773 }
774 ASSERT(false && "unknown action type");
775 return WebRtcSession::kOffer;
776}
777
778bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
779 if (state() == STATE_INIT) {
780 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
781 << "without any offer (local or remote) "
782 << "session description.";
783 return false;
784 }
785
786 if (!candidate) {
787 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL";
788 return false;
789 }
790
791 if (!local_description() || !remote_description()) {
792 LOG(LS_INFO) << "ProcessIceMessage: Remote description not set, "
793 << "save the candidate for later use.";
794 saved_candidates_.push_back(
795 new JsepIceCandidate(candidate->sdp_mid(), candidate->sdp_mline_index(),
796 candidate->candidate()));
797 return true;
798 }
799
800 // Add this candidate to the remote session description.
801 if (!remote_desc_->AddCandidate(candidate)) {
802 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used";
803 return false;
804 }
805
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000806 return UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807}
808
809bool WebRtcSession::GetTrackIdBySsrc(uint32 ssrc, std::string* id) {
810 if (GetLocalTrackId(ssrc, id)) {
811 if (GetRemoteTrackId(ssrc, id)) {
812 LOG(LS_WARNING) << "SSRC " << ssrc
813 << " exists in both local and remote descriptions";
814 return true; // We return the remote track id.
815 }
816 return true;
817 } else {
818 return GetRemoteTrackId(ssrc, id);
819 }
820}
821
822bool WebRtcSession::GetLocalTrackId(uint32 ssrc, std::string* track_id) {
823 if (!BaseSession::local_description())
824 return false;
825 return webrtc::GetTrackIdBySsrc(
826 BaseSession::local_description(), ssrc, track_id);
827}
828
829bool WebRtcSession::GetRemoteTrackId(uint32 ssrc, std::string* track_id) {
830 if (!BaseSession::remote_description())
831 return false;
832 return webrtc::GetTrackIdBySsrc(
833 BaseSession::remote_description(), ssrc, track_id);
834}
835
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000836std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000838 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 return desc.str();
840}
841
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000842void WebRtcSession::SetAudioPlayout(uint32 ssrc, bool enable,
843 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844 ASSERT(signaling_thread()->IsCurrent());
845 if (!voice_channel_) {
846 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
847 return;
848 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000849 if (!voice_channel_->SetRemoteRenderer(ssrc, renderer)) {
850 // SetRenderer() can fail if the ssrc does not match any playout channel.
851 LOG(LS_ERROR) << "SetAudioPlayout: ssrc is incorrect: " << ssrc;
852 return;
853 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854 if (!voice_channel_->SetOutputScaling(ssrc, enable ? 1 : 0, enable ? 1 : 0)) {
855 // Allow that SetOutputScaling fail if |enable| is false but assert
856 // otherwise. This in the normal case when the underlying media channel has
857 // already been deleted.
858 ASSERT(enable == false);
859 }
860}
861
862void WebRtcSession::SetAudioSend(uint32 ssrc, bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000863 const cricket::AudioOptions& options,
864 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000865 ASSERT(signaling_thread()->IsCurrent());
866 if (!voice_channel_) {
867 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
868 return;
869 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000870 if (!voice_channel_->SetLocalRenderer(ssrc, renderer)) {
871 // SetRenderer() can fail if the ssrc does not match any send channel.
872 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
873 return;
874 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 if (!voice_channel_->MuteStream(ssrc, !enable)) {
876 // Allow that MuteStream fail if |enable| is false but assert otherwise.
877 // This in the normal case when the underlying media channel has already
878 // been deleted.
879 ASSERT(enable == false);
880 return;
881 }
882 if (enable)
883 voice_channel_->SetChannelOptions(options);
884}
885
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000886void WebRtcSession::SetAudioPlayoutVolume(uint32 ssrc, double volume) {
887 ASSERT(signaling_thread()->IsCurrent());
888 ASSERT(volume >= 0 && volume <= 10);
889 if (!voice_channel_) {
890 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
891 return;
892 }
893
894 if (!voice_channel_->SetOutputScaling(ssrc, volume, volume))
895 ASSERT(false);
896}
897
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000898bool WebRtcSession::SetCaptureDevice(uint32 ssrc,
899 cricket::VideoCapturer* camera) {
900 ASSERT(signaling_thread()->IsCurrent());
901
902 if (!video_channel_.get()) {
903 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
904 // support video.
905 LOG(LS_WARNING) << "Video not used in this call.";
906 return false;
907 }
908 if (!video_channel_->SetCapturer(ssrc, camera)) {
909 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
910 // This in the normal case when the underlying media channel has already
911 // been deleted.
912 ASSERT(camera == NULL);
913 return false;
914 }
915 return true;
916}
917
918void WebRtcSession::SetVideoPlayout(uint32 ssrc,
919 bool enable,
920 cricket::VideoRenderer* renderer) {
921 ASSERT(signaling_thread()->IsCurrent());
922 if (!video_channel_) {
923 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
924 return;
925 }
926 if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) {
927 // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise.
928 // This in the normal case when the underlying media channel has already
929 // been deleted.
930 ASSERT(renderer == NULL);
931 }
932}
933
934void WebRtcSession::SetVideoSend(uint32 ssrc, bool enable,
935 const cricket::VideoOptions* options) {
936 ASSERT(signaling_thread()->IsCurrent());
937 if (!video_channel_) {
938 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
939 return;
940 }
941 if (!video_channel_->MuteStream(ssrc, !enable)) {
942 // Allow that MuteStream fail if |enable| is false but assert otherwise.
943 // This in the normal case when the underlying media channel has already
944 // been deleted.
945 ASSERT(enable == false);
946 return;
947 }
948 if (enable && options)
949 video_channel_->SetChannelOptions(*options);
950}
951
952bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
953 ASSERT(signaling_thread()->IsCurrent());
954 if (!voice_channel_) {
955 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
956 return false;
957 }
958 uint32 send_ssrc = 0;
959 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
960 // exists.
961 if (!GetAudioSsrcByTrackId(BaseSession::local_description(), track_id,
962 &send_ssrc)) {
963 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
964 return false;
965 }
966 return voice_channel_->CanInsertDtmf();
967}
968
969bool WebRtcSession::InsertDtmf(const std::string& track_id,
970 int code, int duration) {
971 ASSERT(signaling_thread()->IsCurrent());
972 if (!voice_channel_) {
973 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
974 return false;
975 }
976 uint32 send_ssrc = 0;
977 if (!VERIFY(GetAudioSsrcByTrackId(BaseSession::local_description(),
978 track_id, &send_ssrc))) {
979 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
980 return false;
981 }
982 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration,
983 cricket::DF_SEND)) {
984 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
985 return false;
986 }
987 return true;
988}
989
990sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
991 return &SignalVoiceChannelDestroyed;
992}
993
wu@webrtc.org78187522013-10-07 23:32:02 +0000994bool WebRtcSession::SendData(const cricket::SendDataParams& params,
995 const talk_base::Buffer& payload,
996 cricket::SendDataResult* result) {
997 if (!data_channel_.get()) {
998 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
999 return false;
1000 }
1001 return data_channel_->SendData(params, payload, result);
1002}
1003
1004bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
1005 if (!data_channel_.get()) {
1006 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1007 return false;
1008 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001009 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1010 &DataChannel::OnChannelReady);
1011 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1012 &DataChannel::OnDataReceived);
wu@webrtc.org78187522013-10-07 23:32:02 +00001013 return true;
1014}
1015
1016void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001017 if (!data_channel_.get()) {
1018 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1019 return;
1020 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001021 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1022 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
1023}
1024
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001025void WebRtcSession::AddSctpDataStream(uint32 sid) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001026 if (!data_channel_.get()) {
1027 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1028 return;
1029 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001030 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1031 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001032}
1033
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001034void WebRtcSession::RemoveSctpDataStream(uint32 sid) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001035 if (!data_channel_.get()) {
1036 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1037 << "NULL.";
1038 return;
1039 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001040 data_channel_->RemoveRecvStream(sid);
1041 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001042}
1043
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001044bool WebRtcSession::ReadyToSendData() const {
1045 return data_channel_.get() && data_channel_->ready_to_send_data();
1046}
1047
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001048talk_base::scoped_refptr<DataChannel> WebRtcSession::CreateDataChannel(
wu@webrtc.org78187522013-10-07 23:32:02 +00001049 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001050 const InternalDataChannelInit* config) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001051 if (state() == STATE_RECEIVEDTERMINATE) {
1052 return NULL;
1053 }
1054 if (data_channel_type_ == cricket::DCT_NONE) {
1055 LOG(LS_ERROR) << "CreateDataChannel: Data is not supported in this call.";
1056 return NULL;
1057 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001058 InternalDataChannelInit new_config =
1059 config ? (*config) : InternalDataChannelInit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001060 if (data_channel_type_ == cricket::DCT_SCTP) {
1061 if (new_config.id < 0) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001062 talk_base::SSLRole role;
1063 if (GetSslRole(&role) &&
1064 !mediastream_signaling_->AllocateSctpSid(role, &new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001065 LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel.";
1066 return NULL;
1067 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001068 } else if (!mediastream_signaling_->IsSctpSidAvailable(new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001069 LOG(LS_ERROR) << "Failed to create a SCTP data channel "
1070 << "because the id is already in use or out of range.";
1071 return NULL;
1072 }
1073 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001074
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001075 talk_base::scoped_refptr<DataChannel> channel(DataChannel::Create(
1076 this, data_channel_type_, label, new_config));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001077 if (channel && !mediastream_signaling_->AddDataChannel(channel))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001078 return NULL;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001079
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001080 return channel;
1081}
1082
1083cricket::DataChannelType WebRtcSession::data_channel_type() const {
1084 return data_channel_type_;
1085}
1086
wu@webrtc.org91053e72013-08-10 07:18:04 +00001087bool WebRtcSession::IceRestartPending() const {
1088 return ice_restart_latch_->Get();
1089}
1090
1091void WebRtcSession::ResetIceRestartLatch() {
1092 ice_restart_latch_->Reset();
1093}
1094
1095void WebRtcSession::OnIdentityReady(talk_base::SSLIdentity* identity) {
1096 SetIdentity(identity);
1097}
1098
1099bool WebRtcSession::waiting_for_identity() const {
1100 return webrtc_session_desc_factory_->waiting_for_identity();
1101}
1102
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001103void WebRtcSession::SetIceConnectionState(
1104 PeerConnectionInterface::IceConnectionState state) {
1105 if (ice_connection_state_ == state) {
1106 return;
1107 }
1108
1109 // ASSERT that the requested transition is allowed. Note that
1110 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1111 // within PeerConnection). This switch statement should compile away when
1112 // ASSERTs are disabled.
1113 switch (ice_connection_state_) {
1114 case PeerConnectionInterface::kIceConnectionNew:
1115 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1116 break;
1117 case PeerConnectionInterface::kIceConnectionChecking:
1118 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1119 state == PeerConnectionInterface::kIceConnectionConnected);
1120 break;
1121 case PeerConnectionInterface::kIceConnectionConnected:
1122 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1123 state == PeerConnectionInterface::kIceConnectionChecking ||
1124 state == PeerConnectionInterface::kIceConnectionCompleted);
1125 break;
1126 case PeerConnectionInterface::kIceConnectionCompleted:
1127 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1128 state == PeerConnectionInterface::kIceConnectionDisconnected);
1129 break;
1130 case PeerConnectionInterface::kIceConnectionFailed:
1131 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1132 break;
1133 case PeerConnectionInterface::kIceConnectionDisconnected:
1134 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1135 state == PeerConnectionInterface::kIceConnectionConnected ||
1136 state == PeerConnectionInterface::kIceConnectionCompleted ||
1137 state == PeerConnectionInterface::kIceConnectionFailed);
1138 break;
1139 case PeerConnectionInterface::kIceConnectionClosed:
1140 ASSERT(false);
1141 break;
1142 default:
1143 ASSERT(false);
1144 break;
1145 }
1146
1147 ice_connection_state_ = state;
1148 if (ice_observer_) {
1149 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1150 }
1151}
1152
1153void WebRtcSession::OnTransportRequestSignaling(
1154 cricket::Transport* transport) {
1155 ASSERT(signaling_thread()->IsCurrent());
1156 transport->OnSignalingReady();
1157 if (ice_observer_) {
1158 ice_observer_->OnIceGatheringChange(
1159 PeerConnectionInterface::kIceGatheringGathering);
1160 }
1161}
1162
1163void WebRtcSession::OnTransportConnecting(cricket::Transport* transport) {
1164 ASSERT(signaling_thread()->IsCurrent());
1165 // start monitoring for the write state of the transport.
1166 OnTransportWritable(transport);
1167}
1168
1169void WebRtcSession::OnTransportWritable(cricket::Transport* transport) {
1170 ASSERT(signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001171 if (transport->all_channels_writable()) {
henrike@webrtc.org05376342014-03-10 15:53:12 +00001172 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001173 } else if (transport->HasChannels()) {
1174 // If the current state is Connected or Completed, then there were writable
1175 // channels but now there are not, so the next state must be Disconnected.
1176 if (ice_connection_state_ ==
1177 PeerConnectionInterface::kIceConnectionConnected ||
1178 ice_connection_state_ ==
1179 PeerConnectionInterface::kIceConnectionCompleted) {
1180 SetIceConnectionState(
1181 PeerConnectionInterface::kIceConnectionDisconnected);
1182 }
1183 }
1184}
1185
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001186void WebRtcSession::OnTransportCompleted(cricket::Transport* transport) {
1187 ASSERT(signaling_thread()->IsCurrent());
1188 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1189}
1190
1191void WebRtcSession::OnTransportFailed(cricket::Transport* transport) {
1192 ASSERT(signaling_thread()->IsCurrent());
1193 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1194}
1195
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001196void WebRtcSession::OnTransportProxyCandidatesReady(
1197 cricket::TransportProxy* proxy, const cricket::Candidates& candidates) {
1198 ASSERT(signaling_thread()->IsCurrent());
1199 ProcessNewLocalCandidate(proxy->content_name(), candidates);
1200}
1201
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001202void WebRtcSession::OnCandidatesAllocationDone() {
1203 ASSERT(signaling_thread()->IsCurrent());
1204 if (ice_observer_) {
1205 ice_observer_->OnIceGatheringChange(
1206 PeerConnectionInterface::kIceGatheringComplete);
1207 ice_observer_->OnIceComplete();
1208 }
1209}
1210
1211// Enabling voice and video channel.
1212void WebRtcSession::EnableChannels() {
1213 if (voice_channel_ && !voice_channel_->enabled())
1214 voice_channel_->Enable(true);
1215
1216 if (video_channel_ && !video_channel_->enabled())
1217 video_channel_->Enable(true);
1218
1219 if (data_channel_.get() && !data_channel_->enabled())
1220 data_channel_->Enable(true);
1221}
1222
1223void WebRtcSession::ProcessNewLocalCandidate(
1224 const std::string& content_name,
1225 const cricket::Candidates& candidates) {
1226 int sdp_mline_index;
1227 if (!GetLocalCandidateMediaIndex(content_name, &sdp_mline_index)) {
1228 LOG(LS_ERROR) << "ProcessNewLocalCandidate: content name "
1229 << content_name << " not found";
1230 return;
1231 }
1232
1233 for (cricket::Candidates::const_iterator citer = candidates.begin();
1234 citer != candidates.end(); ++citer) {
1235 // Use content_name as the candidate media id.
1236 JsepIceCandidate candidate(content_name, sdp_mline_index, *citer);
1237 if (ice_observer_) {
1238 ice_observer_->OnIceCandidate(&candidate);
1239 }
1240 if (local_desc_) {
1241 local_desc_->AddCandidate(&candidate);
1242 }
1243 }
1244}
1245
1246// Returns the media index for a local ice candidate given the content name.
1247bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1248 int* sdp_mline_index) {
1249 if (!BaseSession::local_description() || !sdp_mline_index)
1250 return false;
1251
1252 bool content_found = false;
1253 const ContentInfos& contents = BaseSession::local_description()->contents();
1254 for (size_t index = 0; index < contents.size(); ++index) {
1255 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001256 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001257 content_found = true;
1258 break;
1259 }
1260 }
1261 return content_found;
1262}
1263
1264bool WebRtcSession::UseCandidatesInSessionDescription(
1265 const SessionDescriptionInterface* remote_desc) {
1266 if (!remote_desc)
1267 return true;
1268 bool ret = true;
1269 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1270 const IceCandidateCollection* candidates = remote_desc->candidates(m);
1271 for (size_t n = 0; n < candidates->count(); ++n) {
1272 ret = UseCandidate(candidates->at(n));
1273 if (!ret)
1274 break;
1275 }
1276 }
1277 return ret;
1278}
1279
1280bool WebRtcSession::UseCandidate(
1281 const IceCandidateInterface* candidate) {
1282
1283 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
1284 size_t remote_content_size =
1285 BaseSession::remote_description()->contents().size();
1286 if (mediacontent_index >= remote_content_size) {
1287 LOG(LS_ERROR)
1288 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1289 return false;
1290 }
1291
1292 cricket::ContentInfo content =
1293 BaseSession::remote_description()->contents()[mediacontent_index];
1294 std::vector<cricket::Candidate> candidates;
1295 candidates.push_back(candidate->candidate());
1296 // Invoking BaseSession method to handle remote candidates.
1297 std::string error;
1298 if (OnRemoteCandidates(content.name, candidates, &error)) {
1299 // Candidates successfully submitted for checking.
1300 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1301 ice_connection_state_ ==
1302 PeerConnectionInterface::kIceConnectionDisconnected) {
1303 // If state is New, then the session has just gotten its first remote ICE
1304 // candidates, so go to Checking.
1305 // If state is Disconnected, the session is re-using old candidates or
1306 // receiving additional ones, so go to Checking.
1307 // If state is Connected, stay Connected.
1308 // TODO(bemasc): If state is Connected, and the new candidates are for a
1309 // newly added transport, then the state actually _should_ move to
1310 // checking. Add a way to distinguish that case.
1311 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1312 }
1313 // TODO(bemasc): If state is Completed, go back to Connected.
1314 } else {
1315 LOG(LS_WARNING) << error;
1316 }
1317 return true;
1318}
1319
1320void WebRtcSession::RemoveUnusedChannelsAndTransports(
1321 const SessionDescription* desc) {
1322 const cricket::ContentInfo* voice_info =
1323 cricket::GetFirstAudioContent(desc);
1324 if ((!voice_info || voice_info->rejected) && voice_channel_) {
1325 mediastream_signaling_->OnAudioChannelClose();
1326 SignalVoiceChannelDestroyed();
1327 const std::string content_name = voice_channel_->content_name();
1328 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
1329 DestroyTransportProxy(content_name);
1330 }
1331
1332 const cricket::ContentInfo* video_info =
1333 cricket::GetFirstVideoContent(desc);
1334 if ((!video_info || video_info->rejected) && video_channel_) {
1335 mediastream_signaling_->OnVideoChannelClose();
1336 SignalVideoChannelDestroyed();
1337 const std::string content_name = video_channel_->content_name();
1338 channel_manager_->DestroyVideoChannel(video_channel_.release());
1339 DestroyTransportProxy(content_name);
1340 }
1341
1342 const cricket::ContentInfo* data_info =
1343 cricket::GetFirstDataContent(desc);
1344 if ((!data_info || data_info->rejected) && data_channel_) {
1345 mediastream_signaling_->OnDataChannelClose();
1346 SignalDataChannelDestroyed();
1347 const std::string content_name = data_channel_->content_name();
1348 channel_manager_->DestroyDataChannel(data_channel_.release());
1349 DestroyTransportProxy(content_name);
1350 }
1351}
1352
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001353// TODO(mallinath) - Add a correct error code if the channels are not creatued
1354// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001355bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
1356 // Disabling the BUNDLE flag in PortAllocator if offer disabled it.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001357 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1358 if (state() == STATE_INIT && !bundle_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001359 port_allocator()->set_flags(port_allocator()->flags() &
1360 ~cricket::PORTALLOCATOR_ENABLE_BUNDLE);
1361 }
1362
1363 // Creating the media channels and transport proxies.
1364 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1365 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001366 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001367 LOG(LS_ERROR) << "Failed to create voice channel.";
1368 return false;
1369 }
1370 }
1371
1372 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1373 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001374 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001375 LOG(LS_ERROR) << "Failed to create video channel.";
1376 return false;
1377 }
1378 }
1379
1380 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1381 if (data_channel_type_ != cricket::DCT_NONE &&
1382 data && !data->rejected && !data_channel_.get()) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001383 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001384 LOG(LS_ERROR) << "Failed to create data channel.";
1385 return false;
1386 }
1387 }
1388
1389 return true;
1390}
1391
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001392bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001393 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001394 this, content->name, true));
wu@webrtc.orgde305012013-10-31 15:40:38 +00001395 if (!voice_channel_.get())
1396 return false;
1397
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00001398 voice_channel_->SetChannelOptions(audio_options_);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001399 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001400}
1401
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001402bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001403 video_channel_.reset(channel_manager_->CreateVideoChannel(
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001404 this, content->name, true, voice_channel_.get()));
wu@webrtc.orgde305012013-10-31 15:40:38 +00001405 if (!video_channel_.get())
1406 return false;
1407
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00001408 video_channel_->SetChannelOptions(video_options_);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001409 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001410}
1411
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001412bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001413 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001414 data_channel_.reset(channel_manager_->CreateDataChannel(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001415 this, content->name, !sctp, data_channel_type_));
wu@webrtc.org91053e72013-08-10 07:18:04 +00001416 if (!data_channel_.get()) {
1417 return false;
1418 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001419 if (sctp) {
1420 mediastream_signaling_->OnDataTransportCreatedForSctp();
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001421 data_channel_->SignalDataReceived.connect(
1422 this, &WebRtcSession::OnDataChannelMessageReceived);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001423 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001424 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001425}
1426
1427void WebRtcSession::CopySavedCandidates(
1428 SessionDescriptionInterface* dest_desc) {
1429 if (!dest_desc) {
1430 ASSERT(false);
1431 return;
1432 }
1433 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
1434 dest_desc->AddCandidate(saved_candidates_[i]);
1435 delete saved_candidates_[i];
1436 }
1437 saved_candidates_.clear();
1438}
1439
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001440void WebRtcSession::OnDataChannelMessageReceived(
1441 cricket::DataChannel* channel,
1442 const cricket::ReceiveDataParams& params,
1443 const talk_base::Buffer& payload) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001444 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001445 if (params.type == cricket::DMT_CONTROL &&
1446 mediastream_signaling_->IsSctpSidAvailable(params.ssrc)) {
1447 // Received CONTROL on unused sid, process as an OPEN message.
1448 mediastream_signaling_->AddDataChannelFromOpenMessage(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001449 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001450 // otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001451}
1452
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001453// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001454bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001455 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1456 if (!bundle_enabled)
1457 return true;
1458
1459 const cricket::ContentGroup* bundle_group =
1460 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1461 ASSERT(bundle_group != NULL);
1462
1463 const cricket::ContentInfos& contents = desc->contents();
1464 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1465 citer != contents.end(); ++citer) {
1466 const cricket::ContentInfo* content = (&*citer);
1467 ASSERT(content != NULL);
1468 if (bundle_group->HasContentName(content->name) &&
1469 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1470 if (!HasRtcpMuxEnabled(content))
1471 return false;
1472 }
1473 }
1474 // RTCP-MUX is enabled in all the contents.
1475 return true;
1476}
1477
1478bool WebRtcSession::HasRtcpMuxEnabled(
1479 const cricket::ContentInfo* content) {
1480 const cricket::MediaContentDescription* description =
1481 static_cast<cricket::MediaContentDescription*>(content->description);
1482 return description->rtcp_mux();
1483}
1484
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001485bool WebRtcSession::ValidateSessionDescription(
1486 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001487 cricket::ContentSource source, std::string* err_desc) {
1488 std::string type;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001489 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001490 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001491 }
1492
1493 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001494 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001495 }
1496
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001497 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001498 Action action = GetAction(sdesc->type());
1499 if (source == cricket::CS_LOCAL) {
1500 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001501 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001502 } else {
1503 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001504 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001505 }
1506
1507 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001508 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001509 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1510 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001511 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001512 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001513 }
1514
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001515 // Verify ice-ufrag and ice-pwd.
1516 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001517 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001518 }
1519
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001520 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001521 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001522 }
1523
1524 // Verify m-lines in Answer when compared against Offer.
1525 if (action == kAnswer) {
1526 const cricket::SessionDescription* offer_desc =
1527 (source == cricket::CS_LOCAL) ? remote_description()->description() :
1528 local_description()->description();
1529 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001530 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001531 }
1532 }
1533
1534 return true;
1535}
1536
1537bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1538 return ((action == kOffer && state() == STATE_INIT) ||
1539 // update local offer
1540 (action == kOffer && state() == STATE_SENTINITIATE) ||
1541 // update the current ongoing session.
1542 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1543 (action == kOffer && state() == STATE_SENTACCEPT) ||
1544 (action == kOffer && state() == STATE_INPROGRESS) ||
1545 // accept remote offer
1546 (action == kAnswer && state() == STATE_RECEIVEDINITIATE) ||
1547 (action == kAnswer && state() == STATE_SENTPRACCEPT) ||
1548 (action == kPrAnswer && state() == STATE_RECEIVEDINITIATE) ||
1549 (action == kPrAnswer && state() == STATE_SENTPRACCEPT));
1550}
1551
1552bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1553 return ((action == kOffer && state() == STATE_INIT) ||
1554 // update remote offer
1555 (action == kOffer && state() == STATE_RECEIVEDINITIATE) ||
1556 // update the current ongoing session
1557 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1558 (action == kOffer && state() == STATE_SENTACCEPT) ||
1559 (action == kOffer && state() == STATE_INPROGRESS) ||
1560 // accept local offer
1561 (action == kAnswer && state() == STATE_SENTINITIATE) ||
1562 (action == kAnswer && state() == STATE_RECEIVEDPRACCEPT) ||
1563 (action == kPrAnswer && state() == STATE_SENTINITIATE) ||
1564 (action == kPrAnswer && state() == STATE_RECEIVEDPRACCEPT));
1565}
1566
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001567std::string WebRtcSession::GetSessionErrorMsg() {
1568 std::ostringstream desc;
1569 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1570 desc << kSessionErrorDesc << error_desc() << ".";
1571 return desc.str();
1572}
1573
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001574} // namespace webrtc