blob: b41813e413d80b7665738aebed613835426e7c55 [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"
buildbot@webrtc.org41451d42014-05-03 05:39:45 +000044#include "talk/base/stringutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045#include "talk/media/base/constants.h"
46#include "talk/media/base/videocapturer.h"
47#include "talk/session/media/channel.h"
48#include "talk/session/media/channelmanager.h"
49#include "talk/session/media/mediasession.h"
50
51using cricket::ContentInfo;
52using cricket::ContentInfos;
53using cricket::MediaContentDescription;
54using cricket::SessionDescription;
55using cricket::TransportInfo;
56
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057namespace webrtc {
58
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000060const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
61 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000062const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063const char kInvalidCandidates[] = "Description contains invalid candidates.";
64const char kInvalidSdp[] = "Invalid session description.";
65const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000066 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
67const char kPushDownTDFailed[] =
68 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000069const char kSdpWithoutDtlsFingerprint[] =
70 "Called with SDP without DTLS fingerprint.";
71const char kSdpWithoutSdesCrypto[] =
72 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000073const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000074 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000076const char kSessionErrorDesc[] = "Session error description: ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077
78// Compares |answer| against |offer|. Comparision is done
79// for number of m-lines in answer against offer. If matches true will be
80// returned otherwise false.
81static bool VerifyMediaDescriptions(
82 const SessionDescription* answer, const SessionDescription* offer) {
83 if (offer->contents().size() != answer->contents().size())
84 return false;
85
86 for (size_t i = 0; i < offer->contents().size(); ++i) {
87 if ((offer->contents()[i].name) != answer->contents()[i].name) {
88 return false;
89 }
wu@webrtc.org4e393072014-04-07 17:04:35 +000090 const MediaContentDescription* offer_mdesc =
91 static_cast<const MediaContentDescription*>(
92 offer->contents()[i].description);
93 const MediaContentDescription* answer_mdesc =
94 static_cast<const MediaContentDescription*>(
95 answer->contents()[i].description);
96 if (offer_mdesc->type() != answer_mdesc->type()) {
97 return false;
98 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 }
100 return true;
101}
102
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103// Checks that each non-rejected content has SDES crypto keys or a DTLS
104// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
105// keys, will be caught in Transport negotiation, and backstopped by Channel's
106// |secure_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000107static bool VerifyCrypto(const SessionDescription* desc,
108 bool dtls_enabled,
109 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110 const ContentInfos& contents = desc->contents();
111 for (size_t index = 0; index < contents.size(); ++index) {
112 const ContentInfo* cinfo = &contents[index];
113 if (cinfo->rejected) {
114 continue;
115 }
116
117 // If the content isn't rejected, crypto must be present.
118 const MediaContentDescription* media =
119 static_cast<const MediaContentDescription*>(cinfo->description);
120 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
121 if (!media || !tinfo) {
122 // Something is not right.
123 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000124 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 return false;
126 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000127 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000128 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000129 LOG(LS_WARNING) <<
130 "Session description must have DTLS fingerprint if DTLS enabled.";
131 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000132 return false;
133 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000134 } else {
135 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000136 LOG(LS_WARNING) <<
137 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000138 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000139 return false;
140 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141 }
142 }
143
144 return true;
145}
146
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000147// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
148static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
149 const ContentInfos& contents = desc->contents();
150 for (size_t index = 0; index < contents.size(); ++index) {
151 const ContentInfo* cinfo = &contents[index];
152 if (cinfo->rejected) {
153 continue;
154 }
155
156 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
157 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
158 if (!tinfo) {
159 // Something is not right.
160 LOG(LS_ERROR) << kInvalidSdp;
161 return false;
162 }
163 if (tinfo->description.ice_ufrag.empty() ||
164 tinfo->description.ice_pwd.empty()) {
165 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
166 return false;
167 }
168 }
169 return true;
170}
171
wu@webrtc.org91053e72013-08-10 07:18:04 +0000172// Forces |sdesc->crypto_required| to the appropriate state based on the
173// current security policy, to ensure a failure occurs if there is an error
174// in crypto negotiation.
175// Called when processing the local session description.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000176static void UpdateSessionDescriptionSecurePolicy(cricket::CryptoType type,
177 SessionDescription* sdesc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000178 if (!sdesc) {
179 return;
180 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181
wu@webrtc.org91053e72013-08-10 07:18:04 +0000182 // Updating the |crypto_required_| in MediaContentDescription to the
183 // appropriate state based on the current security policy.
184 for (cricket::ContentInfos::iterator iter = sdesc->contents().begin();
185 iter != sdesc->contents().end(); ++iter) {
186 if (cricket::IsMediaContent(&*iter)) {
187 MediaContentDescription* mdesc =
188 static_cast<MediaContentDescription*> (iter->description);
189 if (mdesc) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000190 mdesc->set_crypto_required(type);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000191 }
192 }
193 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194}
195
196static bool GetAudioSsrcByTrackId(
197 const SessionDescription* session_description,
198 const std::string& track_id, uint32 *ssrc) {
199 const cricket::ContentInfo* audio_info =
200 cricket::GetFirstAudioContent(session_description);
201 if (!audio_info) {
202 LOG(LS_ERROR) << "Audio not used in this call";
203 return false;
204 }
205
206 const cricket::MediaContentDescription* audio_content =
207 static_cast<const cricket::MediaContentDescription*>(
208 audio_info->description);
209 cricket::StreamParams stream;
210 if (!cricket::GetStreamByIds(audio_content->streams(), "", track_id,
211 &stream)) {
212 return false;
213 }
214 *ssrc = stream.first_ssrc();
215 return true;
216}
217
218static bool GetTrackIdBySsrc(const SessionDescription* session_description,
219 uint32 ssrc, std::string* track_id) {
220 ASSERT(track_id != NULL);
221
222 cricket::StreamParams stream_out;
223 const cricket::ContentInfo* audio_info =
224 cricket::GetFirstAudioContent(session_description);
225 if (!audio_info) {
226 return false;
227 }
228 const cricket::MediaContentDescription* audio_content =
229 static_cast<const cricket::MediaContentDescription*>(
230 audio_info->description);
231
232 if (cricket::GetStreamBySsrc(audio_content->streams(), ssrc, &stream_out)) {
233 *track_id = stream_out.id;
234 return true;
235 }
236
237 const cricket::ContentInfo* video_info =
238 cricket::GetFirstVideoContent(session_description);
239 if (!video_info) {
240 return false;
241 }
242 const cricket::MediaContentDescription* video_content =
243 static_cast<const cricket::MediaContentDescription*>(
244 video_info->description);
245
246 if (cricket::GetStreamBySsrc(video_content->streams(), ssrc, &stream_out)) {
247 *track_id = stream_out.id;
248 return true;
249 }
250 return false;
251}
252
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000253static bool BadSdp(const std::string& source,
254 const std::string& type,
255 const std::string& reason,
256 std::string* err_desc) {
257 std::ostringstream desc;
258 desc << "Failed to set " << source << " " << type << " sdp: " << reason;
259
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000261 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000263 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264 return false;
265}
266
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000268 const std::string& type,
269 const std::string& reason,
270 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000272 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000274 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 }
276}
277
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000278static bool BadLocalSdp(const std::string& type,
279 const std::string& reason,
280 std::string* err_desc) {
281 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
282}
283
284static bool BadRemoteSdp(const std::string& type,
285 const std::string& reason,
286 std::string* err_desc) {
287 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
288}
289
290static bool BadOfferSdp(cricket::ContentSource source,
291 const std::string& reason,
292 std::string* err_desc) {
293 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
294}
295
296static bool BadPranswerSdp(cricket::ContentSource source,
297 const std::string& reason,
298 std::string* err_desc) {
299 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
300 reason, err_desc);
301}
302
303static bool BadAnswerSdp(cricket::ContentSource source,
304 const std::string& reason,
305 std::string* err_desc) {
306 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307}
308
309#define GET_STRING_OF_STATE(state) \
310 case cricket::BaseSession::state: \
311 result = #state; \
312 break;
313
314static std::string GetStateString(cricket::BaseSession::State state) {
315 std::string result;
316 switch (state) {
317 GET_STRING_OF_STATE(STATE_INIT)
318 GET_STRING_OF_STATE(STATE_SENTINITIATE)
319 GET_STRING_OF_STATE(STATE_RECEIVEDINITIATE)
320 GET_STRING_OF_STATE(STATE_SENTPRACCEPT)
321 GET_STRING_OF_STATE(STATE_SENTACCEPT)
322 GET_STRING_OF_STATE(STATE_RECEIVEDPRACCEPT)
323 GET_STRING_OF_STATE(STATE_RECEIVEDACCEPT)
324 GET_STRING_OF_STATE(STATE_SENTMODIFY)
325 GET_STRING_OF_STATE(STATE_RECEIVEDMODIFY)
326 GET_STRING_OF_STATE(STATE_SENTREJECT)
327 GET_STRING_OF_STATE(STATE_RECEIVEDREJECT)
328 GET_STRING_OF_STATE(STATE_SENTREDIRECT)
329 GET_STRING_OF_STATE(STATE_SENTTERMINATE)
330 GET_STRING_OF_STATE(STATE_RECEIVEDTERMINATE)
331 GET_STRING_OF_STATE(STATE_INPROGRESS)
332 GET_STRING_OF_STATE(STATE_DEINIT)
333 default:
334 ASSERT(false);
335 break;
336 }
337 return result;
338}
339
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000340#define GET_STRING_OF_ERROR_CODE(err) \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341 case cricket::BaseSession::err: \
342 result = #err; \
343 break;
344
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000345static std::string GetErrorCodeString(cricket::BaseSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346 std::string result;
347 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000348 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
349 GET_STRING_OF_ERROR_CODE(ERROR_TIME)
350 GET_STRING_OF_ERROR_CODE(ERROR_RESPONSE)
351 GET_STRING_OF_ERROR_CODE(ERROR_NETWORK)
352 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
353 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000354 default:
355 ASSERT(false);
356 break;
357 }
358 return result;
359}
360
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000361static std::string MakeErrorString(const std::string& error,
362 const std::string& desc) {
363 std::ostringstream ret;
364 ret << error << " " << desc;
365 return ret.str();
366}
367
368static std::string MakeTdErrorString(const std::string& desc) {
369 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000370}
371
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000372// Set |option| to the highest-priority value of |key| in the optional
373// constraints if the key is found and has a valid value.
buildbot@webrtc.orgdb563902014-06-13 13:05:48 +0000374template<typename T>
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000375static void SetOptionFromOptionalConstraint(
376 const MediaConstraintsInterface* constraints,
buildbot@webrtc.orgdb563902014-06-13 13:05:48 +0000377 const std::string& key, cricket::Settable<T>* option) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000378 if (!constraints) {
379 return;
380 }
381 std::string string_value;
buildbot@webrtc.orgdb563902014-06-13 13:05:48 +0000382 T value;
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000383 if (constraints->GetOptional().FindFirst(key, &string_value)) {
384 if (talk_base::FromString(string_value, &value)) {
385 option->Set(value);
386 }
387 }
388}
389
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000390// Help class used to remember if a a remote peer has requested ice restart by
391// by sending a description with new ice ufrag and password.
392class IceRestartAnswerLatch {
393 public:
394 IceRestartAnswerLatch() : ice_restart_(false) { }
395
wu@webrtc.org91053e72013-08-10 07:18:04 +0000396 // Returns true if CheckForRemoteIceRestart has been called with a new session
397 // description where ice password and ufrag has changed since last time
398 // Reset() was called.
399 bool Get() const {
400 return ice_restart_;
401 }
402
403 void Reset() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404 if (ice_restart_) {
405 ice_restart_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407 }
408
409 void CheckForRemoteIceRestart(
410 const SessionDescriptionInterface* old_desc,
411 const SessionDescriptionInterface* new_desc) {
412 if (!old_desc || new_desc->type() != SessionDescriptionInterface::kOffer) {
413 return;
414 }
415 const SessionDescription* new_sd = new_desc->description();
416 const SessionDescription* old_sd = old_desc->description();
417 const ContentInfos& contents = new_sd->contents();
418 for (size_t index = 0; index < contents.size(); ++index) {
419 const ContentInfo* cinfo = &contents[index];
420 if (cinfo->rejected) {
421 continue;
422 }
423 // If the content isn't rejected, check if ufrag and password has
424 // changed.
425 const cricket::TransportDescription* new_transport_desc =
426 new_sd->GetTransportDescriptionByName(cinfo->name);
427 const cricket::TransportDescription* old_transport_desc =
428 old_sd->GetTransportDescriptionByName(cinfo->name);
429 if (!new_transport_desc || !old_transport_desc) {
430 // No transport description exist. This is not an ice restart.
431 continue;
432 }
433 if (new_transport_desc->ice_pwd != old_transport_desc->ice_pwd &&
434 new_transport_desc->ice_ufrag != old_transport_desc->ice_ufrag) {
435 LOG(LS_INFO) << "Remote peer request ice restart.";
436 ice_restart_ = true;
437 break;
438 }
439 }
440 }
441
442 private:
443 bool ice_restart_;
444};
445
wu@webrtc.org91053e72013-08-10 07:18:04 +0000446WebRtcSession::WebRtcSession(
447 cricket::ChannelManager* channel_manager,
448 talk_base::Thread* signaling_thread,
449 talk_base::Thread* worker_thread,
450 cricket::PortAllocator* port_allocator,
451 MediaStreamSignaling* mediastream_signaling)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000452 : cricket::BaseSession(signaling_thread, worker_thread, port_allocator,
453 talk_base::ToString(talk_base::CreateRandomId64() &
454 LLONG_MAX),
455 cricket::NS_JINGLE_RTP, false),
456 // RFC 3264: The numeric value of the session id and version in the
457 // o line MUST be representable with a "64 bit signed integer".
458 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
459 channel_manager_(channel_manager),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000460 mediastream_signaling_(mediastream_signaling),
461 ice_observer_(NULL),
462 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000464 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465 data_channel_type_(cricket::DCT_NONE),
466 ice_restart_latch_(new IceRestartAnswerLatch) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000467}
468
469WebRtcSession::~WebRtcSession() {
470 if (voice_channel_.get()) {
471 SignalVoiceChannelDestroyed();
472 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
473 }
474 if (video_channel_.get()) {
475 SignalVideoChannelDestroyed();
476 channel_manager_->DestroyVideoChannel(video_channel_.release());
477 }
478 if (data_channel_.get()) {
479 SignalDataChannelDestroyed();
480 channel_manager_->DestroyDataChannel(data_channel_.release());
481 }
482 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
483 delete saved_candidates_[i];
484 }
485 delete identity();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486}
487
wu@webrtc.org91053e72013-08-10 07:18:04 +0000488bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000489 const PeerConnectionFactoryInterface::Options& options,
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000490 const MediaConstraintsInterface* constraints,
491 DTLSIdentityServiceInterface* dtls_identity_service,
492 PeerConnectionInterface::IceTransportsType ice_transport) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493 // TODO(perkj): Take |constraints| into consideration. Return false if not all
494 // mandatory constraints can be fulfilled. Note that |constraints|
495 // can be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496 bool value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000497
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000498 if (options.disable_encryption) {
499 dtls_enabled_ = false;
500 } else {
501 // Enable DTLS by default if |dtls_identity_service| is valid.
502 dtls_enabled_ = (dtls_identity_service != NULL);
503 // |constraints| can override the default |dtls_enabled_| value.
504 if (FindConstraint(
505 constraints,
506 MediaConstraintsInterface::kEnableDtlsSrtp,
507 &value, NULL)) {
508 dtls_enabled_ = value;
509 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000510 }
511
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000513 // It takes precendence over the disable_sctp_data_channels
514 // PeerConnectionFactoryInterface::Options.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515 if (FindConstraint(
516 constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
517 &value, NULL) && value) {
518 LOG(LS_INFO) << "Allowing RTP data engine.";
519 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000520 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000521 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000522 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000523 LOG(LS_INFO) << "Allowing SCTP data engine.";
524 data_channel_type_ = cricket::DCT_SCTP;
525 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526 }
527 if (data_channel_type_ != cricket::DCT_NONE) {
528 mediastream_signaling_->SetDataChannelFactory(this);
529 }
530
wu@webrtc.orgde305012013-10-31 15:40:38 +0000531 // Find DSCP constraint.
532 if (FindConstraint(
533 constraints,
534 MediaConstraintsInterface::kEnableDscp,
535 &value, NULL)) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000536 audio_options_.dscp.Set(value);
537 video_options_.dscp.Set(value);
538 }
539
540 // Find Suspend Below Min Bitrate constraint.
541 if (FindConstraint(
542 constraints,
543 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
544 &value,
545 NULL)) {
546 video_options_.suspend_below_min_bitrate.Set(value);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000547 }
548
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +0000549 if (FindConstraint(
550 constraints,
551 MediaConstraintsInterface::kSkipEncodingUnusedStreams,
552 &value,
553 NULL)) {
554 video_options_.skip_encoding_unused_streams.Set(value);
555 }
556
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000557 SetOptionFromOptionalConstraint(constraints,
558 MediaConstraintsInterface::kScreencastMinBitrate,
559 &video_options_.screencast_min_bitrate);
560
561 // Find constraints for cpu overuse detection.
562 SetOptionFromOptionalConstraint(constraints,
563 MediaConstraintsInterface::kCpuUnderuseThreshold,
564 &video_options_.cpu_underuse_threshold);
565 SetOptionFromOptionalConstraint(constraints,
566 MediaConstraintsInterface::kCpuOveruseThreshold,
567 &video_options_.cpu_overuse_threshold);
buildbot@webrtc.orgdb563902014-06-13 13:05:48 +0000568 SetOptionFromOptionalConstraint(constraints,
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000569 MediaConstraintsInterface::kCpuOveruseDetection,
buildbot@webrtc.orgdb563902014-06-13 13:05:48 +0000570 &video_options_.cpu_overuse_detection);
571 SetOptionFromOptionalConstraint(constraints,
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000572 MediaConstraintsInterface::kCpuOveruseEncodeUsage,
buildbot@webrtc.orgdb563902014-06-13 13:05:48 +0000573 &video_options_.cpu_overuse_encode_usage);
574
575 // Find payload padding constraint.
576 SetOptionFromOptionalConstraint(constraints,
577 MediaConstraintsInterface::kPayloadPadding,
578 &video_options_.use_payload_padding);
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +0000579
580 // Find improved wifi bwe constraint.
581 if (FindConstraint(
582 constraints,
583 MediaConstraintsInterface::kImprovedWifiBwe,
584 &value,
585 NULL)) {
586 video_options_.use_improved_wifi_bandwidth_estimator.Set(value);
buildbot@webrtc.orgff6a3d92014-05-08 16:16:41 +0000587 } else {
588 // Enable by default if the constraint is not set.
589 video_options_.use_improved_wifi_bandwidth_estimator.Set(true);
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +0000590 }
591
buildbot@webrtc.orgdb563902014-06-13 13:05:48 +0000592 SetOptionFromOptionalConstraint(constraints,
593 MediaConstraintsInterface::kHighStartBitrate,
594 &video_options_.video_start_bitrate);
wu@webrtc.orgcfe5e9c2014-03-27 17:03:58 +0000595
596 if (FindConstraint(
597 constraints,
598 MediaConstraintsInterface::kVeryHighBitrate,
599 &value,
600 NULL)) {
601 video_options_.video_highest_bitrate.Set(
602 cricket::VideoOptions::VERY_HIGH);
603 } else if (FindConstraint(
604 constraints,
605 MediaConstraintsInterface::kHighBitrate,
606 &value,
607 NULL)) {
608 video_options_.video_highest_bitrate.Set(
609 cricket::VideoOptions::HIGH);
610 }
611
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612 const cricket::VideoCodec default_codec(
613 JsepSessionDescription::kDefaultVideoCodecId,
614 JsepSessionDescription::kDefaultVideoCodecName,
615 JsepSessionDescription::kMaxVideoCodecWidth,
616 JsepSessionDescription::kMaxVideoCodecHeight,
617 JsepSessionDescription::kDefaultVideoCodecFramerate,
618 JsepSessionDescription::kDefaultVideoCodecPreference);
619 channel_manager_->SetDefaultVideoEncoderConfig(
620 cricket::VideoEncoderConfig(default_codec));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000621
622 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
623 signaling_thread(),
624 channel_manager_,
625 mediastream_signaling_,
626 dtls_identity_service,
627 this,
628 id(),
629 data_channel_type_,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000630 dtls_enabled_));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000631
632 webrtc_session_desc_factory_->SignalIdentityReady.connect(
633 this, &WebRtcSession::OnIdentityReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000634
wu@webrtc.org97077a32013-10-25 21:18:33 +0000635 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000636 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000637 }
638
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 return true;
640}
641
642void WebRtcSession::Terminate() {
643 SetState(STATE_RECEIVEDTERMINATE);
644 RemoveUnusedChannelsAndTransports(NULL);
645 ASSERT(voice_channel_.get() == NULL);
646 ASSERT(video_channel_.get() == NULL);
647 ASSERT(data_channel_.get() == NULL);
648}
649
650bool WebRtcSession::StartCandidatesAllocation() {
651 // SpeculativelyConnectTransportChannels, will call ConnectChannels method
652 // from TransportProxy to start gathering ice candidates.
653 SpeculativelyConnectAllTransportChannels();
654 if (!saved_candidates_.empty()) {
655 // If there are saved candidates which arrived before local description is
656 // set, copy those to remote description.
657 CopySavedCandidates(remote_desc_.get());
658 }
659 // Push remote candidates present in remote description to transport channels.
660 UseCandidatesInSessionDescription(remote_desc_.get());
661 return true;
662}
663
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000664void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
665 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000666}
667
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000668cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
669 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000670}
671
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000672bool WebRtcSession::GetSslRole(talk_base::SSLRole* role) {
673 if (local_description() == NULL || remote_description() == NULL) {
674 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
675 << "SSL Role of the session.";
676 return false;
677 }
678
679 // TODO(mallinath) - Return role of each transport, as role may differ from
680 // one another.
681 // In current implementaion we just return the role of first transport in the
682 // transport map.
683 for (cricket::TransportMap::const_iterator iter = transport_proxies().begin();
684 iter != transport_proxies().end(); ++iter) {
685 if (iter->second->impl()) {
686 return iter->second->impl()->GetSslRole(role);
687 }
688 }
689 return false;
690}
691
wu@webrtc.org91053e72013-08-10 07:18:04 +0000692void WebRtcSession::CreateOffer(CreateSessionDescriptionObserver* observer,
693 const MediaConstraintsInterface* constraints) {
694 webrtc_session_desc_factory_->CreateOffer(observer, constraints);
695}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696
wu@webrtc.org91053e72013-08-10 07:18:04 +0000697void WebRtcSession::CreateAnswer(CreateSessionDescriptionObserver* observer,
698 const MediaConstraintsInterface* constraints) {
699 webrtc_session_desc_factory_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700}
701
702bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
703 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000704 // Takes the ownership of |desc| regardless of the result.
705 talk_base::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
706
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000707 // Validate SDP.
708 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
709 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710 }
711
712 // Update the initiator flag if this session is the initiator.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000713 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714 if (state() == STATE_INIT && action == kOffer) {
715 set_initiator(true);
716 }
717
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000718 cricket::SecurePolicy sdes_policy =
719 webrtc_session_desc_factory_->SdesPolicy();
720 cricket::CryptoType crypto_required = dtls_enabled_ ?
721 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
722 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000724 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725
726 set_local_description(desc->description()->Copy());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000727 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000728
729 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000730 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 // TODO(mallinath) - Handle CreateChannel failure, as new local description
732 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000733 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000734 }
735
736 // Remove channel and transport proxies, if MediaContentDescription is
737 // rejected.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000738 RemoveUnusedChannelsAndTransports(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000739
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000740 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741 return false;
742 }
743 // Kick starting the ice candidates allocation.
744 StartCandidatesAllocation();
745
746 // Update state and SSRC of local MediaStreams and DataChannels based on the
747 // local session description.
748 mediastream_signaling_->OnLocalDescriptionChanged(local_desc_.get());
749
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000750 talk_base::SSLRole role;
751 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
752 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
753 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000754 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000755 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756 }
757 return true;
758}
759
760bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
761 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000762 // Takes the ownership of |desc| regardless of the result.
763 talk_base::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
764
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000765 // Validate SDP.
766 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
767 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768 }
769
770 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000771 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772 if (action == kOffer && !CreateChannels(desc->description())) {
773 // TODO(mallinath) - Handle CreateChannel failure, as new local description
774 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000775 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776 }
777
778 // Remove channel and transport proxies, if MediaContentDescription is
779 // rejected.
780 RemoveUnusedChannelsAndTransports(desc->description());
781
782 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
783 // is called.
784 set_remote_description(desc->description()->Copy());
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000785 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786 return false;
787 }
788
789 // Update remote MediaStreams.
790 mediastream_signaling_->OnRemoteDescriptionChanged(desc);
791 if (local_description() && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000792 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793 }
794
795 // Copy all saved candidates.
796 CopySavedCandidates(desc);
797 // We retain all received candidates.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000798 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
799 remote_desc_.get(), desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800 // Check if this new SessionDescription contains new ice ufrag and password
801 // that indicates the remote peer requests ice restart.
802 ice_restart_latch_->CheckForRemoteIceRestart(remote_desc_.get(),
803 desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000804 remote_desc_.reset(desc_temp.release());
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000805
806 talk_base::SSLRole role;
807 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
808 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
809 }
810
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000812 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 }
814 return true;
815}
816
817bool WebRtcSession::UpdateSessionState(
818 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819 std::string* err_desc) {
820 // If there's already a pending error then no state transition should happen.
821 // But all call-sites should be verifying this before calling us!
822 ASSERT(error() == cricket::BaseSession::ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000823 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000825 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
826 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 }
828 SetState(source == cricket::CS_LOCAL ?
829 STATE_SENTINITIATE : STATE_RECEIVEDINITIATE);
830 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000831 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000832 }
833 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000834 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
835 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 }
837 EnableChannels();
838 SetState(source == cricket::CS_LOCAL ?
839 STATE_SENTPRACCEPT : STATE_RECEIVEDPRACCEPT);
840 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000841 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 }
843 } else if (action == kAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000844 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
845 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000846 }
847 MaybeEnableMuxingSupport();
848 EnableChannels();
849 SetState(source == cricket::CS_LOCAL ?
850 STATE_SENTACCEPT : STATE_RECEIVEDACCEPT);
851 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000852 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 }
854 }
855 return true;
856}
857
858WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
859 if (type == SessionDescriptionInterface::kOffer) {
860 return WebRtcSession::kOffer;
861 } else if (type == SessionDescriptionInterface::kPrAnswer) {
862 return WebRtcSession::kPrAnswer;
863 } else if (type == SessionDescriptionInterface::kAnswer) {
864 return WebRtcSession::kAnswer;
865 }
866 ASSERT(false && "unknown action type");
867 return WebRtcSession::kOffer;
868}
869
870bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
871 if (state() == STATE_INIT) {
872 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
873 << "without any offer (local or remote) "
874 << "session description.";
875 return false;
876 }
877
878 if (!candidate) {
879 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL";
880 return false;
881 }
882
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000883 cricket::TransportProxy* transport_proxy = NULL;
884 if (remote_description()) {
885 size_t mediacontent_index =
886 static_cast<size_t>(candidate->sdp_mline_index());
887 size_t remote_content_size =
888 BaseSession::remote_description()->contents().size();
889 if (mediacontent_index >= remote_content_size) {
890 LOG(LS_ERROR)
891 << "ProcessIceMessage: Invalid candidate media index.";
892 return false;
893 }
894
895 cricket::ContentInfo content =
896 BaseSession::remote_description()->contents()[mediacontent_index];
897 transport_proxy = GetTransportProxy(content.name);
898 }
899
900 // We need to check the local/remote description for the Transport instead of
901 // the session, because a new Transport added during renegotiation may have
902 // them unset while the session has them set from the previou negotiation. Not
903 // doing so may trigger the auto generation of transport description and mess
904 // up DTLS identity information, ICE credential, etc.
905 if (!transport_proxy || !(transport_proxy->local_description_set() &&
906 transport_proxy->remote_description_set())) {
907 LOG(LS_INFO) << "ProcessIceMessage: Local/Remote description not set "
908 << "on the Transport, save the candidate for later use.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 saved_candidates_.push_back(
910 new JsepIceCandidate(candidate->sdp_mid(), candidate->sdp_mline_index(),
911 candidate->candidate()));
912 return true;
913 }
914
915 // Add this candidate to the remote session description.
916 if (!remote_desc_->AddCandidate(candidate)) {
917 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used";
918 return false;
919 }
920
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000921 return UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922}
923
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000924bool WebRtcSession::UpdateIce(PeerConnectionInterface::IceTransportsType type) {
925 return false;
926}
927
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000928bool WebRtcSession::GetLocalTrackIdBySsrc(uint32 ssrc, std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000929 if (!BaseSession::local_description())
930 return false;
931 return webrtc::GetTrackIdBySsrc(
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000932 BaseSession::local_description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000933}
934
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000935bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32 ssrc, std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 if (!BaseSession::remote_description())
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000937 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938 return webrtc::GetTrackIdBySsrc(
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000939 BaseSession::remote_description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000940}
941
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000942std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000943 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000944 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945 return desc.str();
946}
947
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000948void WebRtcSession::SetAudioPlayout(uint32 ssrc, bool enable,
949 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950 ASSERT(signaling_thread()->IsCurrent());
951 if (!voice_channel_) {
952 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
953 return;
954 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000955 if (!voice_channel_->SetRemoteRenderer(ssrc, renderer)) {
956 // SetRenderer() can fail if the ssrc does not match any playout channel.
957 LOG(LS_ERROR) << "SetAudioPlayout: ssrc is incorrect: " << ssrc;
958 return;
959 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000960 if (!voice_channel_->SetOutputScaling(ssrc, enable ? 1 : 0, enable ? 1 : 0)) {
961 // Allow that SetOutputScaling fail if |enable| is false but assert
962 // otherwise. This in the normal case when the underlying media channel has
963 // already been deleted.
964 ASSERT(enable == false);
965 }
966}
967
968void WebRtcSession::SetAudioSend(uint32 ssrc, bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000969 const cricket::AudioOptions& options,
970 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971 ASSERT(signaling_thread()->IsCurrent());
972 if (!voice_channel_) {
973 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
974 return;
975 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000976 if (!voice_channel_->SetLocalRenderer(ssrc, renderer)) {
977 // SetRenderer() can fail if the ssrc does not match any send channel.
978 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
979 return;
980 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000981 if (!voice_channel_->MuteStream(ssrc, !enable)) {
982 // Allow that MuteStream fail if |enable| is false but assert otherwise.
983 // This in the normal case when the underlying media channel has already
984 // been deleted.
985 ASSERT(enable == false);
986 return;
987 }
988 if (enable)
989 voice_channel_->SetChannelOptions(options);
990}
991
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000992void WebRtcSession::SetAudioPlayoutVolume(uint32 ssrc, double volume) {
993 ASSERT(signaling_thread()->IsCurrent());
994 ASSERT(volume >= 0 && volume <= 10);
995 if (!voice_channel_) {
996 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
997 return;
998 }
999
1000 if (!voice_channel_->SetOutputScaling(ssrc, volume, volume))
1001 ASSERT(false);
1002}
1003
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004bool WebRtcSession::SetCaptureDevice(uint32 ssrc,
1005 cricket::VideoCapturer* camera) {
1006 ASSERT(signaling_thread()->IsCurrent());
1007
1008 if (!video_channel_.get()) {
1009 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1010 // support video.
1011 LOG(LS_WARNING) << "Video not used in this call.";
1012 return false;
1013 }
1014 if (!video_channel_->SetCapturer(ssrc, camera)) {
1015 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1016 // This in the normal case when the underlying media channel has already
1017 // been deleted.
1018 ASSERT(camera == NULL);
1019 return false;
1020 }
1021 return true;
1022}
1023
1024void WebRtcSession::SetVideoPlayout(uint32 ssrc,
1025 bool enable,
1026 cricket::VideoRenderer* renderer) {
1027 ASSERT(signaling_thread()->IsCurrent());
1028 if (!video_channel_) {
1029 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1030 return;
1031 }
1032 if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) {
1033 // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise.
1034 // This in the normal case when the underlying media channel has already
1035 // been deleted.
1036 ASSERT(renderer == NULL);
1037 }
1038}
1039
1040void WebRtcSession::SetVideoSend(uint32 ssrc, bool enable,
1041 const cricket::VideoOptions* options) {
1042 ASSERT(signaling_thread()->IsCurrent());
1043 if (!video_channel_) {
1044 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1045 return;
1046 }
1047 if (!video_channel_->MuteStream(ssrc, !enable)) {
1048 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1049 // This in the normal case when the underlying media channel has already
1050 // been deleted.
1051 ASSERT(enable == false);
1052 return;
1053 }
1054 if (enable && options)
1055 video_channel_->SetChannelOptions(*options);
1056}
1057
1058bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1059 ASSERT(signaling_thread()->IsCurrent());
1060 if (!voice_channel_) {
1061 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1062 return false;
1063 }
1064 uint32 send_ssrc = 0;
1065 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1066 // exists.
1067 if (!GetAudioSsrcByTrackId(BaseSession::local_description(), track_id,
1068 &send_ssrc)) {
1069 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1070 return false;
1071 }
1072 return voice_channel_->CanInsertDtmf();
1073}
1074
1075bool WebRtcSession::InsertDtmf(const std::string& track_id,
1076 int code, int duration) {
1077 ASSERT(signaling_thread()->IsCurrent());
1078 if (!voice_channel_) {
1079 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1080 return false;
1081 }
1082 uint32 send_ssrc = 0;
1083 if (!VERIFY(GetAudioSsrcByTrackId(BaseSession::local_description(),
1084 track_id, &send_ssrc))) {
1085 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1086 return false;
1087 }
1088 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration,
1089 cricket::DF_SEND)) {
1090 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1091 return false;
1092 }
1093 return true;
1094}
1095
1096sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
1097 return &SignalVoiceChannelDestroyed;
1098}
1099
wu@webrtc.org78187522013-10-07 23:32:02 +00001100bool WebRtcSession::SendData(const cricket::SendDataParams& params,
1101 const talk_base::Buffer& payload,
1102 cricket::SendDataResult* result) {
1103 if (!data_channel_.get()) {
1104 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1105 return false;
1106 }
1107 return data_channel_->SendData(params, payload, result);
1108}
1109
1110bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
1111 if (!data_channel_.get()) {
1112 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1113 return false;
1114 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001115 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1116 &DataChannel::OnChannelReady);
1117 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1118 &DataChannel::OnDataReceived);
wu@webrtc.org78187522013-10-07 23:32:02 +00001119 return true;
1120}
1121
1122void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001123 if (!data_channel_.get()) {
1124 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1125 return;
1126 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001127 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1128 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
1129}
1130
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001131void WebRtcSession::AddSctpDataStream(uint32 sid) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001132 if (!data_channel_.get()) {
1133 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1134 return;
1135 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001136 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1137 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001138}
1139
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001140void WebRtcSession::RemoveSctpDataStream(uint32 sid) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001141 if (!data_channel_.get()) {
1142 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1143 << "NULL.";
1144 return;
1145 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001146 data_channel_->RemoveRecvStream(sid);
1147 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001148}
1149
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001150bool WebRtcSession::ReadyToSendData() const {
1151 return data_channel_.get() && data_channel_->ready_to_send_data();
1152}
1153
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001154talk_base::scoped_refptr<DataChannel> WebRtcSession::CreateDataChannel(
wu@webrtc.org78187522013-10-07 23:32:02 +00001155 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001156 const InternalDataChannelInit* config) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001157 if (state() == STATE_RECEIVEDTERMINATE) {
1158 return NULL;
1159 }
1160 if (data_channel_type_ == cricket::DCT_NONE) {
1161 LOG(LS_ERROR) << "CreateDataChannel: Data is not supported in this call.";
1162 return NULL;
1163 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001164 InternalDataChannelInit new_config =
1165 config ? (*config) : InternalDataChannelInit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001166 if (data_channel_type_ == cricket::DCT_SCTP) {
1167 if (new_config.id < 0) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001168 talk_base::SSLRole role;
1169 if (GetSslRole(&role) &&
1170 !mediastream_signaling_->AllocateSctpSid(role, &new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001171 LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel.";
1172 return NULL;
1173 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001174 } else if (!mediastream_signaling_->IsSctpSidAvailable(new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001175 LOG(LS_ERROR) << "Failed to create a SCTP data channel "
1176 << "because the id is already in use or out of range.";
1177 return NULL;
1178 }
1179 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001180
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001181 talk_base::scoped_refptr<DataChannel> channel(DataChannel::Create(
1182 this, data_channel_type_, label, new_config));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001183 if (channel && !mediastream_signaling_->AddDataChannel(channel))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001184 return NULL;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001185
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001186 return channel;
1187}
1188
1189cricket::DataChannelType WebRtcSession::data_channel_type() const {
1190 return data_channel_type_;
1191}
1192
wu@webrtc.org91053e72013-08-10 07:18:04 +00001193bool WebRtcSession::IceRestartPending() const {
1194 return ice_restart_latch_->Get();
1195}
1196
1197void WebRtcSession::ResetIceRestartLatch() {
1198 ice_restart_latch_->Reset();
1199}
1200
1201void WebRtcSession::OnIdentityReady(talk_base::SSLIdentity* identity) {
1202 SetIdentity(identity);
1203}
1204
1205bool WebRtcSession::waiting_for_identity() const {
1206 return webrtc_session_desc_factory_->waiting_for_identity();
1207}
1208
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001209void WebRtcSession::SetIceConnectionState(
1210 PeerConnectionInterface::IceConnectionState state) {
1211 if (ice_connection_state_ == state) {
1212 return;
1213 }
1214
1215 // ASSERT that the requested transition is allowed. Note that
1216 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1217 // within PeerConnection). This switch statement should compile away when
1218 // ASSERTs are disabled.
1219 switch (ice_connection_state_) {
1220 case PeerConnectionInterface::kIceConnectionNew:
1221 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1222 break;
1223 case PeerConnectionInterface::kIceConnectionChecking:
1224 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1225 state == PeerConnectionInterface::kIceConnectionConnected);
1226 break;
1227 case PeerConnectionInterface::kIceConnectionConnected:
1228 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1229 state == PeerConnectionInterface::kIceConnectionChecking ||
1230 state == PeerConnectionInterface::kIceConnectionCompleted);
1231 break;
1232 case PeerConnectionInterface::kIceConnectionCompleted:
1233 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1234 state == PeerConnectionInterface::kIceConnectionDisconnected);
1235 break;
1236 case PeerConnectionInterface::kIceConnectionFailed:
1237 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1238 break;
1239 case PeerConnectionInterface::kIceConnectionDisconnected:
1240 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1241 state == PeerConnectionInterface::kIceConnectionConnected ||
1242 state == PeerConnectionInterface::kIceConnectionCompleted ||
1243 state == PeerConnectionInterface::kIceConnectionFailed);
1244 break;
1245 case PeerConnectionInterface::kIceConnectionClosed:
1246 ASSERT(false);
1247 break;
1248 default:
1249 ASSERT(false);
1250 break;
1251 }
1252
1253 ice_connection_state_ = state;
1254 if (ice_observer_) {
1255 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1256 }
1257}
1258
1259void WebRtcSession::OnTransportRequestSignaling(
1260 cricket::Transport* transport) {
1261 ASSERT(signaling_thread()->IsCurrent());
1262 transport->OnSignalingReady();
1263 if (ice_observer_) {
1264 ice_observer_->OnIceGatheringChange(
1265 PeerConnectionInterface::kIceGatheringGathering);
1266 }
1267}
1268
1269void WebRtcSession::OnTransportConnecting(cricket::Transport* transport) {
1270 ASSERT(signaling_thread()->IsCurrent());
1271 // start monitoring for the write state of the transport.
1272 OnTransportWritable(transport);
1273}
1274
1275void WebRtcSession::OnTransportWritable(cricket::Transport* transport) {
1276 ASSERT(signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001277 if (transport->all_channels_writable()) {
henrike@webrtc.org05376342014-03-10 15:53:12 +00001278 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001279 } else if (transport->HasChannels()) {
1280 // If the current state is Connected or Completed, then there were writable
1281 // channels but now there are not, so the next state must be Disconnected.
1282 if (ice_connection_state_ ==
1283 PeerConnectionInterface::kIceConnectionConnected ||
1284 ice_connection_state_ ==
1285 PeerConnectionInterface::kIceConnectionCompleted) {
1286 SetIceConnectionState(
1287 PeerConnectionInterface::kIceConnectionDisconnected);
1288 }
1289 }
1290}
1291
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001292void WebRtcSession::OnTransportCompleted(cricket::Transport* transport) {
1293 ASSERT(signaling_thread()->IsCurrent());
1294 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1295}
1296
1297void WebRtcSession::OnTransportFailed(cricket::Transport* transport) {
1298 ASSERT(signaling_thread()->IsCurrent());
1299 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1300}
1301
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001302void WebRtcSession::OnTransportProxyCandidatesReady(
1303 cricket::TransportProxy* proxy, const cricket::Candidates& candidates) {
1304 ASSERT(signaling_thread()->IsCurrent());
1305 ProcessNewLocalCandidate(proxy->content_name(), candidates);
1306}
1307
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001308void WebRtcSession::OnCandidatesAllocationDone() {
1309 ASSERT(signaling_thread()->IsCurrent());
1310 if (ice_observer_) {
1311 ice_observer_->OnIceGatheringChange(
1312 PeerConnectionInterface::kIceGatheringComplete);
1313 ice_observer_->OnIceComplete();
1314 }
1315}
1316
1317// Enabling voice and video channel.
1318void WebRtcSession::EnableChannels() {
1319 if (voice_channel_ && !voice_channel_->enabled())
1320 voice_channel_->Enable(true);
1321
1322 if (video_channel_ && !video_channel_->enabled())
1323 video_channel_->Enable(true);
1324
1325 if (data_channel_.get() && !data_channel_->enabled())
1326 data_channel_->Enable(true);
1327}
1328
1329void WebRtcSession::ProcessNewLocalCandidate(
1330 const std::string& content_name,
1331 const cricket::Candidates& candidates) {
1332 int sdp_mline_index;
1333 if (!GetLocalCandidateMediaIndex(content_name, &sdp_mline_index)) {
1334 LOG(LS_ERROR) << "ProcessNewLocalCandidate: content name "
1335 << content_name << " not found";
1336 return;
1337 }
1338
1339 for (cricket::Candidates::const_iterator citer = candidates.begin();
1340 citer != candidates.end(); ++citer) {
1341 // Use content_name as the candidate media id.
1342 JsepIceCandidate candidate(content_name, sdp_mline_index, *citer);
1343 if (ice_observer_) {
1344 ice_observer_->OnIceCandidate(&candidate);
1345 }
1346 if (local_desc_) {
1347 local_desc_->AddCandidate(&candidate);
1348 }
1349 }
1350}
1351
1352// Returns the media index for a local ice candidate given the content name.
1353bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1354 int* sdp_mline_index) {
1355 if (!BaseSession::local_description() || !sdp_mline_index)
1356 return false;
1357
1358 bool content_found = false;
1359 const ContentInfos& contents = BaseSession::local_description()->contents();
1360 for (size_t index = 0; index < contents.size(); ++index) {
1361 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001362 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001363 content_found = true;
1364 break;
1365 }
1366 }
1367 return content_found;
1368}
1369
1370bool WebRtcSession::UseCandidatesInSessionDescription(
1371 const SessionDescriptionInterface* remote_desc) {
1372 if (!remote_desc)
1373 return true;
1374 bool ret = true;
1375 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1376 const IceCandidateCollection* candidates = remote_desc->candidates(m);
1377 for (size_t n = 0; n < candidates->count(); ++n) {
1378 ret = UseCandidate(candidates->at(n));
1379 if (!ret)
1380 break;
1381 }
1382 }
1383 return ret;
1384}
1385
1386bool WebRtcSession::UseCandidate(
1387 const IceCandidateInterface* candidate) {
1388
1389 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
1390 size_t remote_content_size =
1391 BaseSession::remote_description()->contents().size();
1392 if (mediacontent_index >= remote_content_size) {
1393 LOG(LS_ERROR)
1394 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1395 return false;
1396 }
1397
1398 cricket::ContentInfo content =
1399 BaseSession::remote_description()->contents()[mediacontent_index];
1400 std::vector<cricket::Candidate> candidates;
1401 candidates.push_back(candidate->candidate());
1402 // Invoking BaseSession method to handle remote candidates.
1403 std::string error;
1404 if (OnRemoteCandidates(content.name, candidates, &error)) {
1405 // Candidates successfully submitted for checking.
1406 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1407 ice_connection_state_ ==
1408 PeerConnectionInterface::kIceConnectionDisconnected) {
1409 // If state is New, then the session has just gotten its first remote ICE
1410 // candidates, so go to Checking.
1411 // If state is Disconnected, the session is re-using old candidates or
1412 // receiving additional ones, so go to Checking.
1413 // If state is Connected, stay Connected.
1414 // TODO(bemasc): If state is Connected, and the new candidates are for a
1415 // newly added transport, then the state actually _should_ move to
1416 // checking. Add a way to distinguish that case.
1417 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1418 }
1419 // TODO(bemasc): If state is Completed, go back to Connected.
1420 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001421 if (!error.empty()) {
1422 LOG(LS_WARNING) << error;
1423 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001424 }
1425 return true;
1426}
1427
1428void WebRtcSession::RemoveUnusedChannelsAndTransports(
1429 const SessionDescription* desc) {
1430 const cricket::ContentInfo* voice_info =
1431 cricket::GetFirstAudioContent(desc);
1432 if ((!voice_info || voice_info->rejected) && voice_channel_) {
1433 mediastream_signaling_->OnAudioChannelClose();
1434 SignalVoiceChannelDestroyed();
1435 const std::string content_name = voice_channel_->content_name();
1436 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
1437 DestroyTransportProxy(content_name);
1438 }
1439
1440 const cricket::ContentInfo* video_info =
1441 cricket::GetFirstVideoContent(desc);
1442 if ((!video_info || video_info->rejected) && video_channel_) {
1443 mediastream_signaling_->OnVideoChannelClose();
1444 SignalVideoChannelDestroyed();
1445 const std::string content_name = video_channel_->content_name();
1446 channel_manager_->DestroyVideoChannel(video_channel_.release());
1447 DestroyTransportProxy(content_name);
1448 }
1449
1450 const cricket::ContentInfo* data_info =
1451 cricket::GetFirstDataContent(desc);
1452 if ((!data_info || data_info->rejected) && data_channel_) {
1453 mediastream_signaling_->OnDataChannelClose();
1454 SignalDataChannelDestroyed();
1455 const std::string content_name = data_channel_->content_name();
1456 channel_manager_->DestroyDataChannel(data_channel_.release());
1457 DestroyTransportProxy(content_name);
1458 }
1459}
1460
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001461// TODO(mallinath) - Add a correct error code if the channels are not creatued
1462// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001463bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
1464 // Disabling the BUNDLE flag in PortAllocator if offer disabled it.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001465 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1466 if (state() == STATE_INIT && !bundle_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001467 port_allocator()->set_flags(port_allocator()->flags() &
1468 ~cricket::PORTALLOCATOR_ENABLE_BUNDLE);
1469 }
1470
1471 // Creating the media channels and transport proxies.
1472 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1473 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001474 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475 LOG(LS_ERROR) << "Failed to create voice channel.";
1476 return false;
1477 }
1478 }
1479
1480 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1481 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001482 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001483 LOG(LS_ERROR) << "Failed to create video channel.";
1484 return false;
1485 }
1486 }
1487
1488 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1489 if (data_channel_type_ != cricket::DCT_NONE &&
1490 data && !data->rejected && !data_channel_.get()) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001491 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001492 LOG(LS_ERROR) << "Failed to create data channel.";
1493 return false;
1494 }
1495 }
1496
1497 return true;
1498}
1499
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001500bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001501 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001502 this, content->name, true));
wu@webrtc.orgde305012013-10-31 15:40:38 +00001503 if (!voice_channel_.get())
1504 return false;
1505
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00001506 voice_channel_->SetChannelOptions(audio_options_);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001507 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001508}
1509
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001510bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001511 video_channel_.reset(channel_manager_->CreateVideoChannel(
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001512 this, content->name, true, voice_channel_.get()));
wu@webrtc.orgde305012013-10-31 15:40:38 +00001513 if (!video_channel_.get())
1514 return false;
1515
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00001516 video_channel_->SetChannelOptions(video_options_);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001517 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001518}
1519
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001520bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001521 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001522 data_channel_.reset(channel_manager_->CreateDataChannel(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001523 this, content->name, !sctp, data_channel_type_));
wu@webrtc.org91053e72013-08-10 07:18:04 +00001524 if (!data_channel_.get()) {
1525 return false;
1526 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001527 if (sctp) {
1528 mediastream_signaling_->OnDataTransportCreatedForSctp();
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001529 data_channel_->SignalDataReceived.connect(
1530 this, &WebRtcSession::OnDataChannelMessageReceived);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00001531 data_channel_->SignalStreamClosedRemotely.connect(
1532 mediastream_signaling_,
1533 &MediaStreamSignaling::OnRemoteSctpDataChannelClosed);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001534 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001535 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001536}
1537
1538void WebRtcSession::CopySavedCandidates(
1539 SessionDescriptionInterface* dest_desc) {
1540 if (!dest_desc) {
1541 ASSERT(false);
1542 return;
1543 }
1544 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
1545 dest_desc->AddCandidate(saved_candidates_[i]);
1546 delete saved_candidates_[i];
1547 }
1548 saved_candidates_.clear();
1549}
1550
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001551void WebRtcSession::OnDataChannelMessageReceived(
1552 cricket::DataChannel* channel,
1553 const cricket::ReceiveDataParams& params,
1554 const talk_base::Buffer& payload) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001555 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001556 if (params.type == cricket::DMT_CONTROL &&
1557 mediastream_signaling_->IsSctpSidAvailable(params.ssrc)) {
1558 // Received CONTROL on unused sid, process as an OPEN message.
1559 mediastream_signaling_->AddDataChannelFromOpenMessage(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001560 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001561 // otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001562}
1563
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001564// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001565bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001566 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1567 if (!bundle_enabled)
1568 return true;
1569
1570 const cricket::ContentGroup* bundle_group =
1571 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1572 ASSERT(bundle_group != NULL);
1573
1574 const cricket::ContentInfos& contents = desc->contents();
1575 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1576 citer != contents.end(); ++citer) {
1577 const cricket::ContentInfo* content = (&*citer);
1578 ASSERT(content != NULL);
1579 if (bundle_group->HasContentName(content->name) &&
1580 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1581 if (!HasRtcpMuxEnabled(content))
1582 return false;
1583 }
1584 }
1585 // RTCP-MUX is enabled in all the contents.
1586 return true;
1587}
1588
1589bool WebRtcSession::HasRtcpMuxEnabled(
1590 const cricket::ContentInfo* content) {
1591 const cricket::MediaContentDescription* description =
1592 static_cast<cricket::MediaContentDescription*>(content->description);
1593 return description->rtcp_mux();
1594}
1595
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001596bool WebRtcSession::ValidateSessionDescription(
1597 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001598 cricket::ContentSource source, std::string* err_desc) {
1599 std::string type;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001600 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001601 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001602 }
1603
1604 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001605 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001606 }
1607
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001608 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001609 Action action = GetAction(sdesc->type());
1610 if (source == cricket::CS_LOCAL) {
1611 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001612 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001613 } else {
1614 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001615 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001616 }
1617
1618 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001619 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001620 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1621 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001622 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001623 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001624 }
1625
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001626 // Verify ice-ufrag and ice-pwd.
1627 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001628 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001629 }
1630
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001631 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001632 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001633 }
1634
1635 // Verify m-lines in Answer when compared against Offer.
1636 if (action == kAnswer) {
1637 const cricket::SessionDescription* offer_desc =
1638 (source == cricket::CS_LOCAL) ? remote_description()->description() :
1639 local_description()->description();
1640 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001641 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001642 }
1643 }
1644
1645 return true;
1646}
1647
1648bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1649 return ((action == kOffer && state() == STATE_INIT) ||
1650 // update local offer
1651 (action == kOffer && state() == STATE_SENTINITIATE) ||
1652 // update the current ongoing session.
1653 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1654 (action == kOffer && state() == STATE_SENTACCEPT) ||
1655 (action == kOffer && state() == STATE_INPROGRESS) ||
1656 // accept remote offer
1657 (action == kAnswer && state() == STATE_RECEIVEDINITIATE) ||
1658 (action == kAnswer && state() == STATE_SENTPRACCEPT) ||
1659 (action == kPrAnswer && state() == STATE_RECEIVEDINITIATE) ||
1660 (action == kPrAnswer && state() == STATE_SENTPRACCEPT));
1661}
1662
1663bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1664 return ((action == kOffer && state() == STATE_INIT) ||
1665 // update remote offer
1666 (action == kOffer && state() == STATE_RECEIVEDINITIATE) ||
1667 // update the current ongoing session
1668 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1669 (action == kOffer && state() == STATE_SENTACCEPT) ||
1670 (action == kOffer && state() == STATE_INPROGRESS) ||
1671 // accept local offer
1672 (action == kAnswer && state() == STATE_SENTINITIATE) ||
1673 (action == kAnswer && state() == STATE_RECEIVEDPRACCEPT) ||
1674 (action == kPrAnswer && state() == STATE_SENTINITIATE) ||
1675 (action == kPrAnswer && state() == STATE_RECEIVEDPRACCEPT));
1676}
1677
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001678std::string WebRtcSession::GetSessionErrorMsg() {
1679 std::ostringstream desc;
1680 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1681 desc << kSessionErrorDesc << error_desc() << ".";
1682 return desc.str();
1683}
1684
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001685} // namespace webrtc