blob: 8eccd65767dcd92190780e2de469d46e9d880759 [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
ossu7bb87ee2017-01-23 04:56:25 -080011#include "webrtc/pc/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/jsep.h"
16#include "webrtc/api/jsepsessiondescription.h"
17#include "webrtc/api/mediaconstraintsinterface.h"
ossu7bb87ee2017-01-23 04:56:25 -080018#include "webrtc/pc/webrtcsession.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020019#include "webrtc/rtc_base/checks.h"
20#include "webrtc/rtc_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
olka3c747662017-08-17 06:50:32 -070033static 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
olka3c747662017-08-17 06:50:32 -070038static 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
olka3c747662017-08-17 06:50:32 -070043// Checks if each Stream within the |streams| has unique id.
44static bool ValidStreams(const MediaSessionOptions::Streams& streams) {
45 MediaSessionOptions::Streams sorted_streams = streams;
46 std::sort(sorted_streams.begin(), sorted_streams.end(), CompareStream);
47 MediaSessionOptions::Streams::iterator it =
48 std::adjacent_find(sorted_streams.begin(), sorted_streams.end(),
49 SameId);
50 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;
kwibergd1fe2812016-04-27 06:47:29 -070067 std::unique_ptr<webrtc::SessionDescriptionInterface> description;
wu@webrtc.org91053e72013-08-10 07:18:04 +000068};
wu@webrtc.org91053e72013-08-10 07:18:04 +000069} // namespace
70
Henrik Boströmd03c23b2016-06-01 11:44:18 +020071void WebRtcCertificateGeneratorCallback::OnFailure() {
72 SignalRequestFailed();
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000073}
74
Henrik Boströmd03c23b2016-06-01 11:44:18 +020075void WebRtcCertificateGeneratorCallback::OnSuccess(
76 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
77 SignalCertificateReady(certificate);
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000078}
79
wu@webrtc.org91053e72013-08-10 07:18:04 +000080// static
81void WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
82 const SessionDescriptionInterface* source_desc,
deadbeef0ed85b22016-02-23 17:24:52 -080083 const std::string& content_name,
wu@webrtc.org91053e72013-08-10 07:18:04 +000084 SessionDescriptionInterface* dest_desc) {
deadbeef0ed85b22016-02-23 17:24:52 -080085 if (!source_desc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +000086 return;
deadbeef0ed85b22016-02-23 17:24:52 -080087 }
88 const cricket::ContentInfos& contents =
89 source_desc->description()->contents();
90 const cricket::ContentInfo* cinfo =
91 source_desc->description()->GetContentByName(content_name);
92 if (!cinfo) {
93 return;
94 }
95 size_t mediasection_index = static_cast<int>(cinfo - &contents[0]);
96 const IceCandidateCollection* source_candidates =
97 source_desc->candidates(mediasection_index);
98 const IceCandidateCollection* dest_candidates =
99 dest_desc->candidates(mediasection_index);
Taylor Brandstetter4eb1ddd2016-03-01 16:21:07 -0800100 if (!source_candidates || !dest_candidates) {
101 return;
102 }
deadbeef0ed85b22016-02-23 17:24:52 -0800103 for (size_t n = 0; n < source_candidates->count(); ++n) {
104 const IceCandidateInterface* new_candidate = source_candidates->at(n);
105 if (!dest_candidates->HasCandidate(new_candidate)) {
106 dest_desc->AddCandidate(source_candidates->at(n));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000107 }
108 }
109}
110
Henrik Boström87713d02015-08-25 09:53:21 +0200111// Private constructor called by other constructors.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000112WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000113 rtc::Thread* signaling_thread,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000114 cricket::ChannelManager* channel_manager,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000115 WebRtcSession* session,
116 const std::string& session_id,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200117 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
118 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate)
wu@webrtc.org91053e72013-08-10 07:18:04 +0000119 : signaling_thread_(signaling_thread),
wu@webrtc.org91053e72013-08-10 07:18:04 +0000120 session_desc_factory_(channel_manager, &transport_desc_factory_),
121 // RFC 4566 suggested a Network Time Protocol (NTP) format timestamp
122 // as the session id and session version. To simplify, it should be fine
123 // to just use a random number as session id and start version from
124 // |kInitSessionVersion|.
125 session_version_(kInitSessionVersion),
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200126 cert_generator_(std::move(cert_generator)),
wu@webrtc.org91053e72013-08-10 07:18:04 +0000127 session_(session),
128 session_id_(session_id),
Henrik Boström87713d02015-08-25 09:53:21 +0200129 certificate_request_state_(CERTIFICATE_NOT_NEEDED) {
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200130 RTC_DCHECK(signaling_thread_);
olka3c747662017-08-17 06:50:32 -0700131 session_desc_factory_.set_add_legacy_streams(false);
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200132 bool dtls_enabled = cert_generator_ || certificate;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000133 // SRTP-SDES is disabled if DTLS is on.
134 SetSdesPolicy(dtls_enabled ? cricket::SEC_DISABLED : cricket::SEC_REQUIRED);
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200135 if (!dtls_enabled) {
136 LOG(LS_VERBOSE) << "DTLS-SRTP disabled.";
137 return;
138 }
139
140 if (certificate) {
141 // Use |certificate|.
142 certificate_request_state_ = CERTIFICATE_WAITING;
143
144 LOG(LS_VERBOSE) << "DTLS-SRTP enabled; has certificate parameter.";
145 // We already have a certificate but we wait to do |SetIdentity|; if we do
146 // it in the constructor then the caller has not had a chance to connect to
147 // |SignalCertificateReady|.
148 signaling_thread_->Post(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700149 RTC_FROM_HERE, this, MSG_USE_CONSTRUCTOR_CERTIFICATE,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200150 new rtc::ScopedRefMessageData<rtc::RTCCertificate>(certificate));
151 } else {
152 // Generate certificate.
153 certificate_request_state_ = CERTIFICATE_WAITING;
154
155 rtc::scoped_refptr<WebRtcCertificateGeneratorCallback> callback(
156 new rtc::RefCountedObject<WebRtcCertificateGeneratorCallback>());
157 callback->SignalRequestFailed.connect(
158 this, &WebRtcSessionDescriptionFactory::OnCertificateRequestFailed);
159 callback->SignalCertificateReady.connect(
160 this, &WebRtcSessionDescriptionFactory::SetCertificate);
161
162 rtc::KeyParams key_params = rtc::KeyParams();
163 LOG(LS_VERBOSE) << "DTLS-SRTP enabled; sending DTLS identity request (key "
164 << "type: " << key_params.type() << ").";
165
166 // Request certificate. This happens asynchronously, so that the caller gets
167 // a chance to connect to |SignalCertificateReady|.
168 cert_generator_->GenerateCertificateAsync(
169 key_params, rtc::Optional<uint64_t>(), callback);
170 }
Henrik Boström87713d02015-08-25 09:53:21 +0200171}
wu@webrtc.org91053e72013-08-10 07:18:04 +0000172
Henrik Boström87713d02015-08-25 09:53:21 +0200173WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory(
174 rtc::Thread* signaling_thread,
175 cricket::ChannelManager* channel_manager,
Henrik Boström87713d02015-08-25 09:53:21 +0200176 WebRtcSession* session,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200177 const std::string& session_id,
178 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator)
Henrik Boström87713d02015-08-25 09:53:21 +0200179 : WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700180 signaling_thread,
181 channel_manager,
deadbeefab9b2d12015-10-14 11:33:11 -0700182 session,
183 session_id,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200184 std::move(cert_generator),
185 nullptr) {
Henrik Boström87713d02015-08-25 09:53:21 +0200186}
187
188WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory(
189 rtc::Thread* signaling_thread,
190 cricket::ChannelManager* channel_manager,
Henrik Boström87713d02015-08-25 09:53:21 +0200191 WebRtcSession* session,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200192 const std::string& session_id,
193 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate)
deadbeefab9b2d12015-10-14 11:33:11 -0700194 : WebRtcSessionDescriptionFactory(signaling_thread,
195 channel_manager,
deadbeefab9b2d12015-10-14 11:33:11 -0700196 session,
197 session_id,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200198 nullptr,
199 certificate) {
henrikg91d6ede2015-09-17 00:24:34 -0700200 RTC_DCHECK(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000201}
202
203WebRtcSessionDescriptionFactory::~WebRtcSessionDescriptionFactory() {
nisseede5da42017-01-12 05:15:36 -0800204 RTC_DCHECK(signaling_thread_->IsCurrent());
tommi0f620f42015-07-09 03:25:02 -0700205
206 // Fail any requests that were asked for before identity generation completed.
207 FailPendingRequests(kFailedDueToSessionShutdown);
208
209 // Process all pending notifications in the message queue. If we don't do
210 // this, requests will linger and not know they succeeded or failed.
211 rtc::MessageList list;
212 signaling_thread_->Clear(this, rtc::MQID_ANY, &list);
Henrik Boström87713d02015-08-25 09:53:21 +0200213 for (auto& msg : list) {
214 if (msg.message_id != MSG_USE_CONSTRUCTOR_CERTIFICATE) {
215 OnMessage(&msg);
216 } else {
217 // Skip MSG_USE_CONSTRUCTOR_CERTIFICATE because we don't want to trigger
218 // SetIdentity-related callbacks in the destructor. This can be a problem
219 // when WebRtcSession listens to the callback but it was the WebRtcSession
220 // destructor that caused WebRtcSessionDescriptionFactory's destruction.
221 // The callback is then ignored, leaking memory allocated by OnMessage for
222 // MSG_USE_CONSTRUCTOR_CERTIFICATE.
223 delete msg.pdata;
224 }
225 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000226}
227
228void WebRtcSessionDescriptionFactory::CreateOffer(
229 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700230 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
231 const cricket::MediaSessionOptions& session_options) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000232 std::string error = "CreateOffer";
Henrik Boström87713d02015-08-25 09:53:21 +0200233 if (certificate_request_state_ == CERTIFICATE_FAILED) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000234 error += kFailedDueToIdentityFailed;
235 LOG(LS_ERROR) << error;
236 PostCreateSessionDescriptionFailed(observer, error);
237 return;
238 }
239
olka3c747662017-08-17 06:50:32 -0700240 if (!ValidStreams(session_options.streams)) {
241 error += " called with invalid media streams.";
wu@webrtc.org91053e72013-08-10 07:18:04 +0000242 LOG(LS_ERROR) << error;
243 PostCreateSessionDescriptionFailed(observer, error);
244 return;
245 }
246
wu@webrtc.org91053e72013-08-10 07:18:04 +0000247 CreateSessionDescriptionRequest request(
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000248 CreateSessionDescriptionRequest::kOffer, observer, session_options);
Henrik Boström87713d02015-08-25 09:53:21 +0200249 if (certificate_request_state_ == CERTIFICATE_WAITING) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000250 create_session_description_requests_.push(request);
251 } else {
nisseede5da42017-01-12 05:15:36 -0800252 RTC_DCHECK(certificate_request_state_ == CERTIFICATE_SUCCEEDED ||
253 certificate_request_state_ == CERTIFICATE_NOT_NEEDED);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000254 InternalCreateOffer(request);
255 }
256}
257
258void WebRtcSessionDescriptionFactory::CreateAnswer(
259 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700260 const cricket::MediaSessionOptions& session_options) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000261 std::string error = "CreateAnswer";
Henrik Boström87713d02015-08-25 09:53:21 +0200262 if (certificate_request_state_ == CERTIFICATE_FAILED) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000263 error += kFailedDueToIdentityFailed;
264 LOG(LS_ERROR) << error;
265 PostCreateSessionDescriptionFailed(observer, error);
266 return;
267 }
268 if (!session_->remote_description()) {
269 error += " can't be called before SetRemoteDescription.";
270 LOG(LS_ERROR) << error;
271 PostCreateSessionDescriptionFailed(observer, error);
272 return;
273 }
274 if (session_->remote_description()->type() !=
275 JsepSessionDescription::kOffer) {
276 error += " failed because remote_description is not an offer.";
277 LOG(LS_ERROR) << error;
278 PostCreateSessionDescriptionFailed(observer, error);
279 return;
280 }
281
olka3c747662017-08-17 06:50:32 -0700282 if (!ValidStreams(session_options.streams)) {
283 error += " called with invalid media streams.";
wu@webrtc.org91053e72013-08-10 07:18:04 +0000284 LOG(LS_ERROR) << error;
285 PostCreateSessionDescriptionFailed(observer, error);
286 return;
287 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000288
289 CreateSessionDescriptionRequest request(
deadbeefab9b2d12015-10-14 11:33:11 -0700290 CreateSessionDescriptionRequest::kAnswer, observer, session_options);
Henrik Boström87713d02015-08-25 09:53:21 +0200291 if (certificate_request_state_ == CERTIFICATE_WAITING) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000292 create_session_description_requests_.push(request);
293 } else {
nisseede5da42017-01-12 05:15:36 -0800294 RTC_DCHECK(certificate_request_state_ == CERTIFICATE_SUCCEEDED ||
295 certificate_request_state_ == CERTIFICATE_NOT_NEEDED);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000296 InternalCreateAnswer(request);
297 }
298}
299
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000300void WebRtcSessionDescriptionFactory::SetSdesPolicy(
301 cricket::SecurePolicy secure_policy) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000302 session_desc_factory_.set_secure(secure_policy);
303}
304
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000305cricket::SecurePolicy WebRtcSessionDescriptionFactory::SdesPolicy() const {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000306 return session_desc_factory_.secure();
307}
308
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000309void WebRtcSessionDescriptionFactory::OnMessage(rtc::Message* msg) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000310 switch (msg->message_id) {
311 case MSG_CREATE_SESSIONDESCRIPTION_SUCCESS: {
312 CreateSessionDescriptionMsg* param =
313 static_cast<CreateSessionDescriptionMsg*>(msg->pdata);
314 param->observer->OnSuccess(param->description.release());
315 delete param;
316 break;
317 }
318 case MSG_CREATE_SESSIONDESCRIPTION_FAILED: {
319 CreateSessionDescriptionMsg* param =
320 static_cast<CreateSessionDescriptionMsg*>(msg->pdata);
321 param->observer->OnFailure(param->error);
322 delete param;
323 break;
324 }
Henrik Boström87713d02015-08-25 09:53:21 +0200325 case MSG_USE_CONSTRUCTOR_CERTIFICATE: {
326 rtc::ScopedRefMessageData<rtc::RTCCertificate>* param =
327 static_cast<rtc::ScopedRefMessageData<rtc::RTCCertificate>*>(
328 msg->pdata);
329 LOG(LS_INFO) << "Using certificate supplied to the constructor.";
Henrik Boströmd8281982015-08-27 10:12:24 +0200330 SetCertificate(param->data());
Henrik Boström87713d02015-08-25 09:53:21 +0200331 delete param;
332 break;
333 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000334 default:
nissec80e7412017-01-11 05:56:46 -0800335 RTC_NOTREACHED();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000336 break;
337 }
338}
339
340void WebRtcSessionDescriptionFactory::InternalCreateOffer(
341 CreateSessionDescriptionRequest request) {
deadbeefd1a38b52016-12-10 13:15:33 -0800342 if (session_->local_description()) {
olka3c747662017-08-17 06:50:32 -0700343 for (const cricket::TransportInfo& transport :
344 session_->local_description()->description()->transport_infos()) {
345 // If the needs-ice-restart flag is set as described by JSEP, we should
346 // generate an offer with a new ufrag/password to trigger an ICE restart.
347 if (session_->NeedsIceRestart(transport.content_name)) {
348 request.options.transport_options[transport.content_name].ice_restart =
349 true;
deadbeefd1a38b52016-12-10 13:15:33 -0800350 }
351 }
352 }
353
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.
nisseede5da42017-01-12 05:15:36 -0800367 RTC_DCHECK(session_version_ + 1 > session_version_);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000368 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 }
deadbeef0ed85b22016-02-23 17:24:52 -0800377 if (session_->local_description()) {
olka3c747662017-08-17 06:50:32 -0700378 for (const cricket::ContentInfo& content :
379 session_->local_description()->description()->contents()) {
380 // Include all local ICE candidates in the SessionDescription unless
381 // an ICE restart was requested.
382 if (!request.options.transport_options[content.name].ice_restart) {
deadbeef0ed85b22016-02-23 17:24:52 -0800383 CopyCandidatesFromSessionDescription(session_->local_description(),
olka3c747662017-08-17 06:50:32 -0700384 content.name, offer);
deadbeef0ed85b22016-02-23 17:24:52 -0800385 }
386 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000387 }
388 PostCreateSessionDescriptionSucceeded(request.observer, offer);
389}
390
391void WebRtcSessionDescriptionFactory::InternalCreateAnswer(
392 CreateSessionDescriptionRequest request) {
deadbeef0ed85b22016-02-23 17:24:52 -0800393 if (session_->remote_description()) {
olka3c747662017-08-17 06:50:32 -0700394 for (const cricket::ContentInfo& content :
395 session_->remote_description()->description()->contents()) {
deadbeef0ed85b22016-02-23 17:24:52 -0800396 // According to http://tools.ietf.org/html/rfc5245#section-9.2.1.1
397 // an answer should also contain new ICE ufrag and password if an offer
398 // has been received with new ufrag and password.
olka3c747662017-08-17 06:50:32 -0700399 request.options.transport_options[content.name].ice_restart =
400 session_->IceRestartPending(content.name);
deadbeef0ed85b22016-02-23 17:24:52 -0800401 // We should pass the current SSL role to the transport description
402 // factory, if there is already an existing ongoing session.
403 rtc::SSLRole ssl_role;
olka3c747662017-08-17 06:50:32 -0700404 if (session_->GetSslRole(content.name, &ssl_role)) {
405 request.options.transport_options[content.name].prefer_passive_role =
deadbeef0ed85b22016-02-23 17:24:52 -0800406 (rtc::SSL_SERVER == ssl_role);
407 }
408 }
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000409 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000410
411 cricket::SessionDescription* desc(session_desc_factory_.CreateAnswer(
deadbeefd59daf82015-10-14 15:02:44 -0700412 session_->remote_description()
413 ? session_->remote_description()->description()
414 : nullptr,
415 request.options, session_->local_description()
416 ? session_->local_description()->description()
417 : nullptr));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000418 // RFC 3264
419 // If the answer is different from the offer in any way (different IP
420 // addresses, ports, etc.), the origin line MUST be different in the answer.
421 // In that case, the version number in the "o=" line of the answer is
422 // unrelated to the version number in the o line of the offer.
423 // Get a new version number by increasing the |session_version_answer_|.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200424 // The |session_version_| is a uint64_t, the wrap around should not happen.
nisseede5da42017-01-12 05:15:36 -0800425 RTC_DCHECK(session_version_ + 1 > session_version_);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000426 JsepSessionDescription* answer(new JsepSessionDescription(
427 JsepSessionDescription::kAnswer));
428 if (!answer->Initialize(desc, session_id_,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000429 rtc::ToString(session_version_++))) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000430 delete answer;
431 PostCreateSessionDescriptionFailed(request.observer,
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000432 "Failed to initialize the answer.");
wu@webrtc.org91053e72013-08-10 07:18:04 +0000433 return;
434 }
deadbeef0ed85b22016-02-23 17:24:52 -0800435 if (session_->local_description()) {
olka3c747662017-08-17 06:50:32 -0700436 for (const cricket::ContentInfo& content :
437 session_->local_description()->description()->contents()) {
438 // Include all local ICE candidates in the SessionDescription unless
439 // the remote peer has requested an ICE restart.
440 if (!request.options.transport_options[content.name].ice_restart) {
deadbeef0ed85b22016-02-23 17:24:52 -0800441 CopyCandidatesFromSessionDescription(session_->local_description(),
olka3c747662017-08-17 06:50:32 -0700442 content.name, answer);
deadbeef0ed85b22016-02-23 17:24:52 -0800443 }
444 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000445 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000446 PostCreateSessionDescriptionSucceeded(request.observer, answer);
447}
448
tommi0f620f42015-07-09 03:25:02 -0700449void WebRtcSessionDescriptionFactory::FailPendingRequests(
450 const std::string& reason) {
nisseede5da42017-01-12 05:15:36 -0800451 RTC_DCHECK(signaling_thread_->IsCurrent());
tommi0f620f42015-07-09 03:25:02 -0700452 while (!create_session_description_requests_.empty()) {
453 const CreateSessionDescriptionRequest& request =
454 create_session_description_requests_.front();
455 PostCreateSessionDescriptionFailed(request.observer,
456 ((request.type == CreateSessionDescriptionRequest::kOffer) ?
457 "CreateOffer" : "CreateAnswer") + reason);
458 create_session_description_requests_.pop();
459 }
460}
461
wu@webrtc.org91053e72013-08-10 07:18:04 +0000462void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionFailed(
463 CreateSessionDescriptionObserver* observer, const std::string& error) {
464 CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer);
465 msg->error = error;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700466 signaling_thread_->Post(RTC_FROM_HERE, this,
467 MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000468 LOG(LS_ERROR) << "Create SDP failed: " << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000469}
470
471void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionSucceeded(
472 CreateSessionDescriptionObserver* observer,
473 SessionDescriptionInterface* description) {
474 CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer);
475 msg->description.reset(description);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700476 signaling_thread_->Post(RTC_FROM_HERE, this,
477 MSG_CREATE_SESSIONDESCRIPTION_SUCCESS, msg);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000478}
479
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200480void WebRtcSessionDescriptionFactory::OnCertificateRequestFailed() {
nisseede5da42017-01-12 05:15:36 -0800481 RTC_DCHECK(signaling_thread_->IsCurrent());
wu@webrtc.org91053e72013-08-10 07:18:04 +0000482
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200483 LOG(LS_ERROR) << "Asynchronous certificate generation request failed.";
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ömd03c23b2016-06-01 11:44:18 +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