blob: 19ee1e6e03c0b920de0ba4fe9a81284c9d254a07 [file] [log] [blame]
wu@webrtc.org91053e72013-08-10 07:18:04 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
wu@webrtc.org91053e72013-08-10 07:18:04 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
wu@webrtc.org91053e72013-08-10 07:18:04 +00009 */
10
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#include "webrtc/api/webrtcsessiondescriptionfactory.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000012
kwiberg0eb15ed2015-12-17 03:04:15 -080013#include <utility>
14
Henrik Kjellander15583c12016-02-10 10:53:12 +010015#include "webrtc/api/dtlsidentitystore.h"
16#include "webrtc/api/jsep.h"
17#include "webrtc/api/jsepsessiondescription.h"
18#include "webrtc/api/mediaconstraintsinterface.h"
19#include "webrtc/api/webrtcsession.h"
Henrik Boström5e56c592015-08-11 10:33:13 +020020#include "webrtc/base/sslidentity.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000021
wu@webrtc.org364f2042013-11-20 21:49:41 +000022using cricket::MediaSessionOptions;
23
wu@webrtc.org91053e72013-08-10 07:18:04 +000024namespace webrtc {
wu@webrtc.org91053e72013-08-10 07:18:04 +000025namespace {
wu@webrtc.org91053e72013-08-10 07:18:04 +000026static const char kFailedDueToIdentityFailed[] =
27 " failed because DTLS identity request failed";
tommi0f620f42015-07-09 03:25:02 -070028static const char kFailedDueToSessionShutdown[] =
29 " failed because the session was shut down";
wu@webrtc.org91053e72013-08-10 07:18:04 +000030
Peter Boström0c4e06b2015-10-07 12:23:21 +020031static const uint64_t kInitSessionVersion = 2;
wu@webrtc.org91053e72013-08-10 07:18:04 +000032
wu@webrtc.org364f2042013-11-20 21:49:41 +000033static bool CompareStream(const MediaSessionOptions::Stream& stream1,
34 const MediaSessionOptions::Stream& stream2) {
35 return stream1.id < stream2.id;
wu@webrtc.org91053e72013-08-10 07:18:04 +000036}
37
wu@webrtc.org364f2042013-11-20 21:49:41 +000038static bool SameId(const MediaSessionOptions::Stream& stream1,
39 const MediaSessionOptions::Stream& stream2) {
40 return stream1.id == stream2.id;
wu@webrtc.org91053e72013-08-10 07:18:04 +000041}
42
43// Checks if each Stream within the |streams| has unique id.
wu@webrtc.org364f2042013-11-20 21:49:41 +000044static bool ValidStreams(const MediaSessionOptions::Streams& streams) {
45 MediaSessionOptions::Streams sorted_streams = streams;
wu@webrtc.org91053e72013-08-10 07:18:04 +000046 std::sort(sorted_streams.begin(), sorted_streams.end(), CompareStream);
wu@webrtc.org364f2042013-11-20 21:49:41 +000047 MediaSessionOptions::Streams::iterator it =
wu@webrtc.org91053e72013-08-10 07:18:04 +000048 std::adjacent_find(sorted_streams.begin(), sorted_streams.end(),
49 SameId);
wu@webrtc.org364f2042013-11-20 21:49:41 +000050 return it == sorted_streams.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +000051}
52
53enum {
54 MSG_CREATE_SESSIONDESCRIPTION_SUCCESS,
Henrik Boström87713d02015-08-25 09:53:21 +020055 MSG_CREATE_SESSIONDESCRIPTION_FAILED,
56 MSG_USE_CONSTRUCTOR_CERTIFICATE
wu@webrtc.org91053e72013-08-10 07:18:04 +000057};
58
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000059struct CreateSessionDescriptionMsg : public rtc::MessageData {
wu@webrtc.org91053e72013-08-10 07:18:04 +000060 explicit CreateSessionDescriptionMsg(
61 webrtc::CreateSessionDescriptionObserver* observer)
62 : observer(observer) {
63 }
64
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000065 rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer;
wu@webrtc.org91053e72013-08-10 07:18:04 +000066 std::string error;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000067 rtc::scoped_ptr<webrtc::SessionDescriptionInterface> description;
wu@webrtc.org91053e72013-08-10 07:18:04 +000068};
wu@webrtc.org91053e72013-08-10 07:18:04 +000069} // namespace
70
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000071void WebRtcIdentityRequestObserver::OnFailure(int error) {
72 SignalRequestFailed(error);
73}
74
75void WebRtcIdentityRequestObserver::OnSuccess(
76 const std::string& der_cert, const std::string& der_private_key) {
77 std::string pem_cert = rtc::SSLIdentity::DerToPem(
78 rtc::kPemTypeCertificate,
79 reinterpret_cast<const unsigned char*>(der_cert.data()),
80 der_cert.length());
81 std::string pem_key = rtc::SSLIdentity::DerToPem(
82 rtc::kPemTypeRsaPrivateKey,
83 reinterpret_cast<const unsigned char*>(der_private_key.data()),
84 der_private_key.length());
Henrik Boströmd8281982015-08-27 10:12:24 +020085 rtc::scoped_ptr<rtc::SSLIdentity> identity(
86 rtc::SSLIdentity::FromPEMStrings(pem_key, pem_cert));
kwiberg0eb15ed2015-12-17 03:04:15 -080087 SignalCertificateReady(rtc::RTCCertificate::Create(std::move(identity)));
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000088}
89
Henrik Boström5e56c592015-08-11 10:33:13 +020090void WebRtcIdentityRequestObserver::OnSuccess(
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000091 rtc::scoped_ptr<rtc::SSLIdentity> identity) {
kwiberg0eb15ed2015-12-17 03:04:15 -080092 SignalCertificateReady(rtc::RTCCertificate::Create(std::move(identity)));
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000093}
94
wu@webrtc.org91053e72013-08-10 07:18:04 +000095// static
96void WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
97 const SessionDescriptionInterface* source_desc,
98 SessionDescriptionInterface* dest_desc) {
99 if (!source_desc)
100 return;
101 for (size_t m = 0; m < source_desc->number_of_mediasections() &&
102 m < dest_desc->number_of_mediasections(); ++m) {
103 const IceCandidateCollection* source_candidates =
104 source_desc->candidates(m);
105 const IceCandidateCollection* dest_candidates = dest_desc->candidates(m);
106 for (size_t n = 0; n < source_candidates->count(); ++n) {
107 const IceCandidateInterface* new_candidate = source_candidates->at(n);
108 if (!dest_candidates->HasCandidate(new_candidate))
109 dest_desc->AddCandidate(source_candidates->at(n));
110 }
111 }
112}
113
Henrik Boström87713d02015-08-25 09:53:21 +0200114// Private constructor called by other constructors.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000115WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000116 rtc::Thread* signaling_thread,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000117 cricket::ChannelManager* channel_manager,
Henrik Boström5e56c592015-08-11 10:33:13 +0200118 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
Henrik Boström87713d02015-08-25 09:53:21 +0200119 const rtc::scoped_refptr<WebRtcIdentityRequestObserver>&
120 identity_request_observer,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000121 WebRtcSession* session,
122 const std::string& session_id,
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000123 bool dtls_enabled)
wu@webrtc.org91053e72013-08-10 07:18:04 +0000124 : signaling_thread_(signaling_thread),
wu@webrtc.org91053e72013-08-10 07:18:04 +0000125 session_desc_factory_(channel_manager, &transport_desc_factory_),
126 // RFC 4566 suggested a Network Time Protocol (NTP) format timestamp
127 // as the session id and session version. To simplify, it should be fine
128 // to just use a random number as session id and start version from
129 // |kInitSessionVersion|.
130 session_version_(kInitSessionVersion),
kwiberg0eb15ed2015-12-17 03:04:15 -0800131 dtls_identity_store_(std::move(dtls_identity_store)),
Henrik Boström87713d02015-08-25 09:53:21 +0200132 identity_request_observer_(identity_request_observer),
wu@webrtc.org91053e72013-08-10 07:18:04 +0000133 session_(session),
134 session_id_(session_id),
Henrik Boström87713d02015-08-25 09:53:21 +0200135 certificate_request_state_(CERTIFICATE_NOT_NEEDED) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000136 session_desc_factory_.set_add_legacy_streams(false);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000137 // SRTP-SDES is disabled if DTLS is on.
138 SetSdesPolicy(dtls_enabled ? cricket::SEC_DISABLED : cricket::SEC_REQUIRED);
Henrik Boström87713d02015-08-25 09:53:21 +0200139}
wu@webrtc.org91053e72013-08-10 07:18:04 +0000140
Henrik Boström87713d02015-08-25 09:53:21 +0200141WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory(
142 rtc::Thread* signaling_thread,
143 cricket::ChannelManager* channel_manager,
Henrik Boström87713d02015-08-25 09:53:21 +0200144 WebRtcSession* session,
deadbeefab9b2d12015-10-14 11:33:11 -0700145 const std::string& session_id)
deadbeefcbecd352015-09-23 11:50:27 -0700146 : WebRtcSessionDescriptionFactory(signaling_thread,
147 channel_manager,
deadbeefcbecd352015-09-23 11:50:27 -0700148 nullptr,
149 nullptr,
150 session,
151 session_id,
deadbeefcbecd352015-09-23 11:50:27 -0700152 false) {
Henrik Boström87713d02015-08-25 09:53:21 +0200153 LOG(LS_VERBOSE) << "DTLS-SRTP disabled.";
154}
wu@webrtc.org91053e72013-08-10 07:18:04 +0000155
Henrik Boström87713d02015-08-25 09:53:21 +0200156WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory(
157 rtc::Thread* signaling_thread,
158 cricket::ChannelManager* channel_manager,
Henrik Boström87713d02015-08-25 09:53:21 +0200159 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
160 WebRtcSession* session,
deadbeefab9b2d12015-10-14 11:33:11 -0700161 const std::string& session_id)
Henrik Boström87713d02015-08-25 09:53:21 +0200162 : WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700163 signaling_thread,
164 channel_manager,
kwiberg0eb15ed2015-12-17 03:04:15 -0800165 std::move(dtls_identity_store),
deadbeefab9b2d12015-10-14 11:33:11 -0700166 new rtc::RefCountedObject<WebRtcIdentityRequestObserver>(),
167 session,
168 session_id,
169 true) {
henrikg91d6ede2015-09-17 00:24:34 -0700170 RTC_DCHECK(dtls_identity_store_);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000171
Henrik Boström87713d02015-08-25 09:53:21 +0200172 certificate_request_state_ = CERTIFICATE_WAITING;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000173
Henrik Boström87713d02015-08-25 09:53:21 +0200174 identity_request_observer_->SignalRequestFailed.connect(
175 this, &WebRtcSessionDescriptionFactory::OnIdentityRequestFailed);
Henrik Boströmd8281982015-08-27 10:12:24 +0200176 identity_request_observer_->SignalCertificateReady.connect(
177 this, &WebRtcSessionDescriptionFactory::SetCertificate);
Henrik Boström87713d02015-08-25 09:53:21 +0200178
179 rtc::KeyType key_type = rtc::KT_DEFAULT;
180 LOG(LS_VERBOSE) << "DTLS-SRTP enabled; sending DTLS identity request (key "
181 << "type: " << key_type << ").";
182
183 // Request identity. This happens asynchronously, so the caller will have a
184 // chance to connect to SignalIdentityReady.
185 dtls_identity_store_->RequestIdentity(key_type, identity_request_observer_);
186}
187
188WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory(
189 rtc::Thread* signaling_thread,
190 cricket::ChannelManager* channel_manager,
Henrik Boström87713d02015-08-25 09:53:21 +0200191 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate,
192 WebRtcSession* session,
deadbeefab9b2d12015-10-14 11:33:11 -0700193 const std::string& session_id)
194 : WebRtcSessionDescriptionFactory(signaling_thread,
195 channel_manager,
196 nullptr,
197 nullptr,
198 session,
199 session_id,
200 true) {
henrikg91d6ede2015-09-17 00:24:34 -0700201 RTC_DCHECK(certificate);
Henrik Boström87713d02015-08-25 09:53:21 +0200202
203 certificate_request_state_ = CERTIFICATE_WAITING;
204
205 LOG(LS_VERBOSE) << "DTLS-SRTP enabled; has certificate parameter.";
206 // We already have a certificate but we wait to do SetIdentity; if we do
207 // it in the constructor then the caller has not had a chance to connect to
208 // SignalIdentityReady.
deadbeefcbecd352015-09-23 11:50:27 -0700209 signaling_thread_->Post(
210 this, MSG_USE_CONSTRUCTOR_CERTIFICATE,
211 new rtc::ScopedRefMessageData<rtc::RTCCertificate>(certificate));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000212}
213
214WebRtcSessionDescriptionFactory::~WebRtcSessionDescriptionFactory() {
tommi0f620f42015-07-09 03:25:02 -0700215 ASSERT(signaling_thread_->IsCurrent());
216
217 // Fail any requests that were asked for before identity generation completed.
218 FailPendingRequests(kFailedDueToSessionShutdown);
219
220 // Process all pending notifications in the message queue. If we don't do
221 // this, requests will linger and not know they succeeded or failed.
222 rtc::MessageList list;
223 signaling_thread_->Clear(this, rtc::MQID_ANY, &list);
Henrik Boström87713d02015-08-25 09:53:21 +0200224 for (auto& msg : list) {
225 if (msg.message_id != MSG_USE_CONSTRUCTOR_CERTIFICATE) {
226 OnMessage(&msg);
227 } else {
228 // Skip MSG_USE_CONSTRUCTOR_CERTIFICATE because we don't want to trigger
229 // SetIdentity-related callbacks in the destructor. This can be a problem
230 // when WebRtcSession listens to the callback but it was the WebRtcSession
231 // destructor that caused WebRtcSessionDescriptionFactory's destruction.
232 // The callback is then ignored, leaking memory allocated by OnMessage for
233 // MSG_USE_CONSTRUCTOR_CERTIFICATE.
234 delete msg.pdata;
235 }
236 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000237}
238
239void WebRtcSessionDescriptionFactory::CreateOffer(
240 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700241 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
242 const cricket::MediaSessionOptions& session_options) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000243 std::string error = "CreateOffer";
Henrik Boström87713d02015-08-25 09:53:21 +0200244 if (certificate_request_state_ == CERTIFICATE_FAILED) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000245 error += kFailedDueToIdentityFailed;
246 LOG(LS_ERROR) << error;
247 PostCreateSessionDescriptionFailed(observer, error);
248 return;
249 }
250
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000251 if (!ValidStreams(session_options.streams)) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000252 error += " called with invalid media streams.";
253 LOG(LS_ERROR) << error;
254 PostCreateSessionDescriptionFailed(observer, error);
255 return;
256 }
257
wu@webrtc.org91053e72013-08-10 07:18:04 +0000258 CreateSessionDescriptionRequest request(
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000259 CreateSessionDescriptionRequest::kOffer, observer, session_options);
Henrik Boström87713d02015-08-25 09:53:21 +0200260 if (certificate_request_state_ == CERTIFICATE_WAITING) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000261 create_session_description_requests_.push(request);
262 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200263 ASSERT(certificate_request_state_ == CERTIFICATE_SUCCEEDED ||
264 certificate_request_state_ == CERTIFICATE_NOT_NEEDED);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000265 InternalCreateOffer(request);
266 }
267}
268
269void WebRtcSessionDescriptionFactory::CreateAnswer(
270 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700271 const MediaConstraintsInterface* constraints,
272 const cricket::MediaSessionOptions& session_options) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000273 std::string error = "CreateAnswer";
Henrik Boström87713d02015-08-25 09:53:21 +0200274 if (certificate_request_state_ == CERTIFICATE_FAILED) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000275 error += kFailedDueToIdentityFailed;
276 LOG(LS_ERROR) << error;
277 PostCreateSessionDescriptionFailed(observer, error);
278 return;
279 }
280 if (!session_->remote_description()) {
281 error += " can't be called before SetRemoteDescription.";
282 LOG(LS_ERROR) << error;
283 PostCreateSessionDescriptionFailed(observer, error);
284 return;
285 }
286 if (session_->remote_description()->type() !=
287 JsepSessionDescription::kOffer) {
288 error += " failed because remote_description is not an offer.";
289 LOG(LS_ERROR) << error;
290 PostCreateSessionDescriptionFailed(observer, error);
291 return;
292 }
293
deadbeefab9b2d12015-10-14 11:33:11 -0700294 if (!ValidStreams(session_options.streams)) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000295 error += " called with invalid media streams.";
296 LOG(LS_ERROR) << error;
297 PostCreateSessionDescriptionFailed(observer, error);
298 return;
299 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000300
301 CreateSessionDescriptionRequest request(
deadbeefab9b2d12015-10-14 11:33:11 -0700302 CreateSessionDescriptionRequest::kAnswer, observer, session_options);
Henrik Boström87713d02015-08-25 09:53:21 +0200303 if (certificate_request_state_ == CERTIFICATE_WAITING) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000304 create_session_description_requests_.push(request);
305 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200306 ASSERT(certificate_request_state_ == CERTIFICATE_SUCCEEDED ||
307 certificate_request_state_ == CERTIFICATE_NOT_NEEDED);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000308 InternalCreateAnswer(request);
309 }
310}
311
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000312void WebRtcSessionDescriptionFactory::SetSdesPolicy(
313 cricket::SecurePolicy secure_policy) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000314 session_desc_factory_.set_secure(secure_policy);
315}
316
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000317cricket::SecurePolicy WebRtcSessionDescriptionFactory::SdesPolicy() const {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000318 return session_desc_factory_.secure();
319}
320
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000321void WebRtcSessionDescriptionFactory::OnMessage(rtc::Message* msg) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000322 switch (msg->message_id) {
323 case MSG_CREATE_SESSIONDESCRIPTION_SUCCESS: {
324 CreateSessionDescriptionMsg* param =
325 static_cast<CreateSessionDescriptionMsg*>(msg->pdata);
326 param->observer->OnSuccess(param->description.release());
327 delete param;
328 break;
329 }
330 case MSG_CREATE_SESSIONDESCRIPTION_FAILED: {
331 CreateSessionDescriptionMsg* param =
332 static_cast<CreateSessionDescriptionMsg*>(msg->pdata);
333 param->observer->OnFailure(param->error);
334 delete param;
335 break;
336 }
Henrik Boström87713d02015-08-25 09:53:21 +0200337 case MSG_USE_CONSTRUCTOR_CERTIFICATE: {
338 rtc::ScopedRefMessageData<rtc::RTCCertificate>* param =
339 static_cast<rtc::ScopedRefMessageData<rtc::RTCCertificate>*>(
340 msg->pdata);
341 LOG(LS_INFO) << "Using certificate supplied to the constructor.";
Henrik Boströmd8281982015-08-27 10:12:24 +0200342 SetCertificate(param->data());
Henrik Boström87713d02015-08-25 09:53:21 +0200343 delete param;
344 break;
345 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000346 default:
347 ASSERT(false);
348 break;
349 }
350}
351
352void WebRtcSessionDescriptionFactory::InternalCreateOffer(
353 CreateSessionDescriptionRequest request) {
deadbeefd59daf82015-10-14 15:02:44 -0700354 cricket::SessionDescription* desc(session_desc_factory_.CreateOffer(
355 request.options, session_->local_description()
356 ? session_->local_description()->description()
357 : nullptr));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000358 // RFC 3264
359 // When issuing an offer that modifies the session,
360 // the "o=" line of the new SDP MUST be identical to that in the
361 // previous SDP, except that the version in the origin field MUST
362 // increment by one from the previous SDP.
363
364 // Just increase the version number by one each time when a new offer
365 // is created regardless if it's identical to the previous one or not.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200366 // The |session_version_| is a uint64_t, the wrap around should not happen.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000367 ASSERT(session_version_ + 1 > session_version_);
368 JsepSessionDescription* offer(new JsepSessionDescription(
369 JsepSessionDescription::kOffer));
370 if (!offer->Initialize(desc, session_id_,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000371 rtc::ToString(session_version_++))) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000372 delete offer;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000373 PostCreateSessionDescriptionFailed(request.observer,
374 "Failed to initialize the offer.");
wu@webrtc.org91053e72013-08-10 07:18:04 +0000375 return;
376 }
377 if (session_->local_description() &&
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800378 !request.options.audio_transport_options.ice_restart &&
379 !request.options.video_transport_options.ice_restart &&
380 !request.options.data_transport_options.ice_restart) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000381 // Include all local ice candidates in the SessionDescription unless
382 // the an ice restart has been requested.
383 CopyCandidatesFromSessionDescription(session_->local_description(), offer);
384 }
385 PostCreateSessionDescriptionSucceeded(request.observer, offer);
386}
387
388void WebRtcSessionDescriptionFactory::InternalCreateAnswer(
389 CreateSessionDescriptionRequest request) {
390 // According to http://tools.ietf.org/html/rfc5245#section-9.2.1.1
391 // an answer should also contain new ice ufrag and password if an offer has
392 // been received with new ufrag and password.
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800393 request.options.audio_transport_options.ice_restart =
394 session_->IceRestartPending();
395 request.options.video_transport_options.ice_restart =
396 session_->IceRestartPending();
397 request.options.data_transport_options.ice_restart =
398 session_->IceRestartPending();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000399 // We should pass current ssl role to the transport description factory, if
400 // there is already an existing ongoing session.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000401 rtc::SSLRole ssl_role;
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800402 if (session_->GetSslRole(session_->voice_channel(), &ssl_role)) {
403 request.options.audio_transport_options.prefer_passive_role =
404 (rtc::SSL_SERVER == ssl_role);
405 }
406 if (session_->GetSslRole(session_->video_channel(), &ssl_role)) {
407 request.options.video_transport_options.prefer_passive_role =
408 (rtc::SSL_SERVER == ssl_role);
409 }
410 if (session_->GetSslRole(session_->data_channel(), &ssl_role)) {
411 request.options.data_transport_options.prefer_passive_role =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000412 (rtc::SSL_SERVER == ssl_role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000413 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000414
415 cricket::SessionDescription* desc(session_desc_factory_.CreateAnswer(
deadbeefd59daf82015-10-14 15:02:44 -0700416 session_->remote_description()
417 ? session_->remote_description()->description()
418 : nullptr,
419 request.options, session_->local_description()
420 ? session_->local_description()->description()
421 : nullptr));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000422 // RFC 3264
423 // If the answer is different from the offer in any way (different IP
424 // addresses, ports, etc.), the origin line MUST be different in the answer.
425 // In that case, the version number in the "o=" line of the answer is
426 // unrelated to the version number in the o line of the offer.
427 // Get a new version number by increasing the |session_version_answer_|.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200428 // The |session_version_| is a uint64_t, the wrap around should not happen.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000429 ASSERT(session_version_ + 1 > session_version_);
430 JsepSessionDescription* answer(new JsepSessionDescription(
431 JsepSessionDescription::kAnswer));
432 if (!answer->Initialize(desc, session_id_,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000433 rtc::ToString(session_version_++))) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000434 delete answer;
435 PostCreateSessionDescriptionFailed(request.observer,
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000436 "Failed to initialize the answer.");
wu@webrtc.org91053e72013-08-10 07:18:04 +0000437 return;
438 }
439 if (session_->local_description() &&
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800440 !request.options.audio_transport_options.ice_restart &&
441 !request.options.video_transport_options.ice_restart &&
442 !request.options.data_transport_options.ice_restart) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000443 // Include all local ice candidates in the SessionDescription unless
444 // the remote peer has requested an ice restart.
445 CopyCandidatesFromSessionDescription(session_->local_description(), answer);
446 }
447 session_->ResetIceRestartLatch();
448 PostCreateSessionDescriptionSucceeded(request.observer, answer);
449}
450
tommi0f620f42015-07-09 03:25:02 -0700451void WebRtcSessionDescriptionFactory::FailPendingRequests(
452 const std::string& reason) {
453 ASSERT(signaling_thread_->IsCurrent());
454 while (!create_session_description_requests_.empty()) {
455 const CreateSessionDescriptionRequest& request =
456 create_session_description_requests_.front();
457 PostCreateSessionDescriptionFailed(request.observer,
458 ((request.type == CreateSessionDescriptionRequest::kOffer) ?
459 "CreateOffer" : "CreateAnswer") + reason);
460 create_session_description_requests_.pop();
461 }
462}
463
wu@webrtc.org91053e72013-08-10 07:18:04 +0000464void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionFailed(
465 CreateSessionDescriptionObserver* observer, const std::string& error) {
466 CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer);
467 msg->error = error;
468 signaling_thread_->Post(this, MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000469 LOG(LS_ERROR) << "Create SDP failed: " << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000470}
471
472void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionSucceeded(
473 CreateSessionDescriptionObserver* observer,
474 SessionDescriptionInterface* description) {
475 CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer);
476 msg->description.reset(description);
477 signaling_thread_->Post(this, MSG_CREATE_SESSIONDESCRIPTION_SUCCESS, msg);
478}
479
480void WebRtcSessionDescriptionFactory::OnIdentityRequestFailed(int error) {
481 ASSERT(signaling_thread_->IsCurrent());
482
483 LOG(LS_ERROR) << "Async identity request failed: error = " << error;
Henrik Boström87713d02015-08-25 09:53:21 +0200484 certificate_request_state_ = CERTIFICATE_FAILED;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000485
tommi0f620f42015-07-09 03:25:02 -0700486 FailPendingRequests(kFailedDueToIdentityFailed);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000487}
488
Henrik Boströmd8281982015-08-27 10:12:24 +0200489void WebRtcSessionDescriptionFactory::SetCertificate(
490 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
henrikg91d6ede2015-09-17 00:24:34 -0700491 RTC_DCHECK(certificate);
Henrik Boströmd8281982015-08-27 10:12:24 +0200492 LOG(LS_VERBOSE) << "Setting new certificate";
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000493
Henrik Boström87713d02015-08-25 09:53:21 +0200494 certificate_request_state_ = CERTIFICATE_SUCCEEDED;
Henrik Boströmd8281982015-08-27 10:12:24 +0200495 SignalCertificateReady(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000496
Henrik Boström3a14bf32015-08-31 09:27:58 +0200497 transport_desc_factory_.set_certificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000498 transport_desc_factory_.set_secure(cricket::SEC_ENABLED);
499
500 while (!create_session_description_requests_.empty()) {
501 if (create_session_description_requests_.front().type ==
502 CreateSessionDescriptionRequest::kOffer) {
503 InternalCreateOffer(create_session_description_requests_.front());
504 } else {
505 InternalCreateAnswer(create_session_description_requests_.front());
506 }
507 create_session_description_requests_.pop();
508 }
509}
wu@webrtc.org91053e72013-08-10 07:18:04 +0000510} // namespace webrtc