blob: bbab6462bc4b508a2f82f11d14fcdbf2fe0d7d04 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/webrtcsessiondescriptionfactory.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000012
kwiberg0eb15ed2015-12-17 03:04:15 -080013#include <utility>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "api/jsep.h"
16#include "api/jsepsessiondescription.h"
17#include "api/mediaconstraintsinterface.h"
Steve Antond5585ca2017-10-23 14:49:26 -070018#include "pc/peerconnection.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/checks.h"
20#include "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
zhihuang1c378ed2017-08-17 14:10:50 -070033static bool CompareSenderOptions(const cricket::SenderOptions& sender1,
34 const cricket::SenderOptions& sender2) {
35 return sender1.track_id < sender2.track_id;
wu@webrtc.org91053e72013-08-10 07:18:04 +000036}
37
zhihuang1c378ed2017-08-17 14:10:50 -070038static bool SameId(const cricket::SenderOptions& sender1,
39 const cricket::SenderOptions& sender2) {
40 return sender1.track_id == sender2.track_id;
wu@webrtc.org91053e72013-08-10 07:18:04 +000041}
42
zhihuang1c378ed2017-08-17 14:10:50 -070043// Check that each sender has a unique ID.
44static bool ValidMediaSessionOptions(
45 const cricket::MediaSessionOptions& session_options) {
46 std::vector<cricket::SenderOptions> sorted_senders;
47 for (const cricket::MediaDescriptionOptions& media_description_options :
48 session_options.media_description_options) {
49 sorted_senders.insert(sorted_senders.end(),
50 media_description_options.sender_options.begin(),
51 media_description_options.sender_options.end());
52 }
53 std::sort(sorted_senders.begin(), sorted_senders.end(), CompareSenderOptions);
54 std::vector<cricket::SenderOptions>::iterator it =
55 std::adjacent_find(sorted_senders.begin(), sorted_senders.end(), SameId);
56 return it == sorted_senders.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +000057}
58
59enum {
60 MSG_CREATE_SESSIONDESCRIPTION_SUCCESS,
Henrik Boström87713d02015-08-25 09:53:21 +020061 MSG_CREATE_SESSIONDESCRIPTION_FAILED,
62 MSG_USE_CONSTRUCTOR_CERTIFICATE
wu@webrtc.org91053e72013-08-10 07:18:04 +000063};
64
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000065struct CreateSessionDescriptionMsg : public rtc::MessageData {
wu@webrtc.org91053e72013-08-10 07:18:04 +000066 explicit CreateSessionDescriptionMsg(
67 webrtc::CreateSessionDescriptionObserver* observer)
68 : observer(observer) {
69 }
70
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000071 rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer;
wu@webrtc.org91053e72013-08-10 07:18:04 +000072 std::string error;
kwibergd1fe2812016-04-27 06:47:29 -070073 std::unique_ptr<webrtc::SessionDescriptionInterface> description;
wu@webrtc.org91053e72013-08-10 07:18:04 +000074};
wu@webrtc.org91053e72013-08-10 07:18:04 +000075} // namespace
76
Henrik Boströmd03c23b2016-06-01 11:44:18 +020077void WebRtcCertificateGeneratorCallback::OnFailure() {
78 SignalRequestFailed();
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000079}
80
Henrik Boströmd03c23b2016-06-01 11:44:18 +020081void WebRtcCertificateGeneratorCallback::OnSuccess(
82 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
83 SignalCertificateReady(certificate);
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000084}
85
wu@webrtc.org91053e72013-08-10 07:18:04 +000086// static
87void WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
88 const SessionDescriptionInterface* source_desc,
deadbeef0ed85b22016-02-23 17:24:52 -080089 const std::string& content_name,
wu@webrtc.org91053e72013-08-10 07:18:04 +000090 SessionDescriptionInterface* dest_desc) {
deadbeef0ed85b22016-02-23 17:24:52 -080091 if (!source_desc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +000092 return;
deadbeef0ed85b22016-02-23 17:24:52 -080093 }
94 const cricket::ContentInfos& contents =
95 source_desc->description()->contents();
96 const cricket::ContentInfo* cinfo =
97 source_desc->description()->GetContentByName(content_name);
98 if (!cinfo) {
99 return;
100 }
101 size_t mediasection_index = static_cast<int>(cinfo - &contents[0]);
102 const IceCandidateCollection* source_candidates =
103 source_desc->candidates(mediasection_index);
104 const IceCandidateCollection* dest_candidates =
105 dest_desc->candidates(mediasection_index);
Taylor Brandstetter4eb1ddd2016-03-01 16:21:07 -0800106 if (!source_candidates || !dest_candidates) {
107 return;
108 }
deadbeef0ed85b22016-02-23 17:24:52 -0800109 for (size_t n = 0; n < source_candidates->count(); ++n) {
110 const IceCandidateInterface* new_candidate = source_candidates->at(n);
111 if (!dest_candidates->HasCandidate(new_candidate)) {
112 dest_desc->AddCandidate(source_candidates->at(n));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000113 }
114 }
115}
116
Henrik Boström87713d02015-08-25 09:53:21 +0200117// Private constructor called by other constructors.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000118WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000119 rtc::Thread* signaling_thread,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000120 cricket::ChannelManager* channel_manager,
Steve Antond5585ca2017-10-23 14:49:26 -0700121 PeerConnection* pc,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000122 const std::string& session_id,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200123 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
124 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate)
wu@webrtc.org91053e72013-08-10 07:18:04 +0000125 : signaling_thread_(signaling_thread),
wu@webrtc.org91053e72013-08-10 07:18:04 +0000126 session_desc_factory_(channel_manager, &transport_desc_factory_),
127 // RFC 4566 suggested a Network Time Protocol (NTP) format timestamp
128 // as the session id and session version. To simplify, it should be fine
129 // to just use a random number as session id and start version from
130 // |kInitSessionVersion|.
131 session_version_(kInitSessionVersion),
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200132 cert_generator_(std::move(cert_generator)),
Steve Antond5585ca2017-10-23 14:49:26 -0700133 pc_(pc),
wu@webrtc.org91053e72013-08-10 07:18:04 +0000134 session_id_(session_id),
Henrik Boström87713d02015-08-25 09:53:21 +0200135 certificate_request_state_(CERTIFICATE_NOT_NEEDED) {
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200136 RTC_DCHECK(signaling_thread_);
Steve Antond5585ca2017-10-23 14:49:26 -0700137 RTC_DCHECK(!(cert_generator_ && certificate));
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200138 bool dtls_enabled = cert_generator_ || certificate;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000139 // SRTP-SDES is disabled if DTLS is on.
140 SetSdesPolicy(dtls_enabled ? cricket::SEC_DISABLED : cricket::SEC_REQUIRED);
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200141 if (!dtls_enabled) {
142 LOG(LS_VERBOSE) << "DTLS-SRTP disabled.";
143 return;
144 }
145
146 if (certificate) {
147 // Use |certificate|.
148 certificate_request_state_ = CERTIFICATE_WAITING;
149
150 LOG(LS_VERBOSE) << "DTLS-SRTP enabled; has certificate parameter.";
151 // We already have a certificate but we wait to do |SetIdentity|; if we do
152 // it in the constructor then the caller has not had a chance to connect to
153 // |SignalCertificateReady|.
154 signaling_thread_->Post(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700155 RTC_FROM_HERE, this, MSG_USE_CONSTRUCTOR_CERTIFICATE,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200156 new rtc::ScopedRefMessageData<rtc::RTCCertificate>(certificate));
157 } else {
158 // Generate certificate.
159 certificate_request_state_ = CERTIFICATE_WAITING;
160
161 rtc::scoped_refptr<WebRtcCertificateGeneratorCallback> callback(
162 new rtc::RefCountedObject<WebRtcCertificateGeneratorCallback>());
163 callback->SignalRequestFailed.connect(
164 this, &WebRtcSessionDescriptionFactory::OnCertificateRequestFailed);
165 callback->SignalCertificateReady.connect(
166 this, &WebRtcSessionDescriptionFactory::SetCertificate);
167
168 rtc::KeyParams key_params = rtc::KeyParams();
169 LOG(LS_VERBOSE) << "DTLS-SRTP enabled; sending DTLS identity request (key "
170 << "type: " << key_params.type() << ").";
171
172 // Request certificate. This happens asynchronously, so that the caller gets
173 // a chance to connect to |SignalCertificateReady|.
174 cert_generator_->GenerateCertificateAsync(
175 key_params, rtc::Optional<uint64_t>(), callback);
176 }
Henrik Boström87713d02015-08-25 09:53:21 +0200177}
wu@webrtc.org91053e72013-08-10 07:18:04 +0000178
wu@webrtc.org91053e72013-08-10 07:18:04 +0000179WebRtcSessionDescriptionFactory::~WebRtcSessionDescriptionFactory() {
nisseede5da42017-01-12 05:15:36 -0800180 RTC_DCHECK(signaling_thread_->IsCurrent());
tommi0f620f42015-07-09 03:25:02 -0700181
182 // Fail any requests that were asked for before identity generation completed.
183 FailPendingRequests(kFailedDueToSessionShutdown);
184
185 // Process all pending notifications in the message queue. If we don't do
186 // this, requests will linger and not know they succeeded or failed.
187 rtc::MessageList list;
188 signaling_thread_->Clear(this, rtc::MQID_ANY, &list);
Henrik Boström87713d02015-08-25 09:53:21 +0200189 for (auto& msg : list) {
190 if (msg.message_id != MSG_USE_CONSTRUCTOR_CERTIFICATE) {
191 OnMessage(&msg);
192 } else {
193 // Skip MSG_USE_CONSTRUCTOR_CERTIFICATE because we don't want to trigger
194 // SetIdentity-related callbacks in the destructor. This can be a problem
195 // when WebRtcSession listens to the callback but it was the WebRtcSession
196 // destructor that caused WebRtcSessionDescriptionFactory's destruction.
197 // The callback is then ignored, leaking memory allocated by OnMessage for
198 // MSG_USE_CONSTRUCTOR_CERTIFICATE.
199 delete msg.pdata;
200 }
201 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000202}
203
204void WebRtcSessionDescriptionFactory::CreateOffer(
205 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700206 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
207 const cricket::MediaSessionOptions& session_options) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000208 std::string error = "CreateOffer";
Henrik Boström87713d02015-08-25 09:53:21 +0200209 if (certificate_request_state_ == CERTIFICATE_FAILED) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000210 error += kFailedDueToIdentityFailed;
211 LOG(LS_ERROR) << error;
212 PostCreateSessionDescriptionFailed(observer, error);
213 return;
214 }
215
zhihuang1c378ed2017-08-17 14:10:50 -0700216 if (!ValidMediaSessionOptions(session_options)) {
217 error += " called with invalid session options";
wu@webrtc.org91053e72013-08-10 07:18:04 +0000218 LOG(LS_ERROR) << error;
219 PostCreateSessionDescriptionFailed(observer, error);
220 return;
221 }
222
wu@webrtc.org91053e72013-08-10 07:18:04 +0000223 CreateSessionDescriptionRequest request(
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000224 CreateSessionDescriptionRequest::kOffer, observer, session_options);
Henrik Boström87713d02015-08-25 09:53:21 +0200225 if (certificate_request_state_ == CERTIFICATE_WAITING) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000226 create_session_description_requests_.push(request);
227 } else {
nisseede5da42017-01-12 05:15:36 -0800228 RTC_DCHECK(certificate_request_state_ == CERTIFICATE_SUCCEEDED ||
229 certificate_request_state_ == CERTIFICATE_NOT_NEEDED);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000230 InternalCreateOffer(request);
231 }
232}
233
234void WebRtcSessionDescriptionFactory::CreateAnswer(
235 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700236 const cricket::MediaSessionOptions& session_options) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000237 std::string error = "CreateAnswer";
Henrik Boström87713d02015-08-25 09:53:21 +0200238 if (certificate_request_state_ == CERTIFICATE_FAILED) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000239 error += kFailedDueToIdentityFailed;
240 LOG(LS_ERROR) << error;
241 PostCreateSessionDescriptionFailed(observer, error);
242 return;
243 }
Steve Antond5585ca2017-10-23 14:49:26 -0700244 if (!pc_->remote_description()) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000245 error += " can't be called before SetRemoteDescription.";
246 LOG(LS_ERROR) << error;
247 PostCreateSessionDescriptionFailed(observer, error);
248 return;
249 }
Steve Antond5585ca2017-10-23 14:49:26 -0700250 if (pc_->remote_description()->type() != JsepSessionDescription::kOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000251 error += " failed because remote_description is not an offer.";
252 LOG(LS_ERROR) << error;
253 PostCreateSessionDescriptionFailed(observer, error);
254 return;
255 }
256
zhihuang1c378ed2017-08-17 14:10:50 -0700257 if (!ValidMediaSessionOptions(session_options)) {
258 error += " called with invalid session options.";
wu@webrtc.org91053e72013-08-10 07:18:04 +0000259 LOG(LS_ERROR) << error;
260 PostCreateSessionDescriptionFailed(observer, error);
261 return;
262 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000263
264 CreateSessionDescriptionRequest request(
deadbeefab9b2d12015-10-14 11:33:11 -0700265 CreateSessionDescriptionRequest::kAnswer, observer, session_options);
Henrik Boström87713d02015-08-25 09:53:21 +0200266 if (certificate_request_state_ == CERTIFICATE_WAITING) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000267 create_session_description_requests_.push(request);
268 } else {
nisseede5da42017-01-12 05:15:36 -0800269 RTC_DCHECK(certificate_request_state_ == CERTIFICATE_SUCCEEDED ||
270 certificate_request_state_ == CERTIFICATE_NOT_NEEDED);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000271 InternalCreateAnswer(request);
272 }
273}
274
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000275void WebRtcSessionDescriptionFactory::SetSdesPolicy(
276 cricket::SecurePolicy secure_policy) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000277 session_desc_factory_.set_secure(secure_policy);
278}
279
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000280cricket::SecurePolicy WebRtcSessionDescriptionFactory::SdesPolicy() const {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000281 return session_desc_factory_.secure();
282}
283
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000284void WebRtcSessionDescriptionFactory::OnMessage(rtc::Message* msg) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000285 switch (msg->message_id) {
286 case MSG_CREATE_SESSIONDESCRIPTION_SUCCESS: {
287 CreateSessionDescriptionMsg* param =
288 static_cast<CreateSessionDescriptionMsg*>(msg->pdata);
289 param->observer->OnSuccess(param->description.release());
290 delete param;
291 break;
292 }
293 case MSG_CREATE_SESSIONDESCRIPTION_FAILED: {
294 CreateSessionDescriptionMsg* param =
295 static_cast<CreateSessionDescriptionMsg*>(msg->pdata);
296 param->observer->OnFailure(param->error);
297 delete param;
298 break;
299 }
Henrik Boström87713d02015-08-25 09:53:21 +0200300 case MSG_USE_CONSTRUCTOR_CERTIFICATE: {
301 rtc::ScopedRefMessageData<rtc::RTCCertificate>* param =
302 static_cast<rtc::ScopedRefMessageData<rtc::RTCCertificate>*>(
303 msg->pdata);
304 LOG(LS_INFO) << "Using certificate supplied to the constructor.";
Henrik Boströmd8281982015-08-27 10:12:24 +0200305 SetCertificate(param->data());
Henrik Boström87713d02015-08-25 09:53:21 +0200306 delete param;
307 break;
308 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000309 default:
nissec80e7412017-01-11 05:56:46 -0800310 RTC_NOTREACHED();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000311 break;
312 }
313}
314
315void WebRtcSessionDescriptionFactory::InternalCreateOffer(
316 CreateSessionDescriptionRequest request) {
Steve Antond5585ca2017-10-23 14:49:26 -0700317 if (pc_->local_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700318 // If the needs-ice-restart flag is set as described by JSEP, we should
319 // generate an offer with a new ufrag/password to trigger an ICE restart.
320 for (cricket::MediaDescriptionOptions& options :
321 request.options.media_description_options) {
Steve Antond5585ca2017-10-23 14:49:26 -0700322 if (pc_->NeedsIceRestart(options.mid)) {
zhihuang1c378ed2017-08-17 14:10:50 -0700323 options.transport_options.ice_restart = true;
deadbeefd1a38b52016-12-10 13:15:33 -0800324 }
325 }
326 }
327
deadbeefd59daf82015-10-14 15:02:44 -0700328 cricket::SessionDescription* desc(session_desc_factory_.CreateOffer(
Steve Antond5585ca2017-10-23 14:49:26 -0700329 request.options, pc_->local_description()
330 ? pc_->local_description()->description()
deadbeefd59daf82015-10-14 15:02:44 -0700331 : nullptr));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000332 // RFC 3264
333 // When issuing an offer that modifies the session,
334 // the "o=" line of the new SDP MUST be identical to that in the
335 // previous SDP, except that the version in the origin field MUST
336 // increment by one from the previous SDP.
337
338 // Just increase the version number by one each time when a new offer
339 // is created regardless if it's identical to the previous one or not.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200340 // The |session_version_| is a uint64_t, the wrap around should not happen.
nisseede5da42017-01-12 05:15:36 -0800341 RTC_DCHECK(session_version_ + 1 > session_version_);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000342 JsepSessionDescription* offer(new JsepSessionDescription(
343 JsepSessionDescription::kOffer));
344 if (!offer->Initialize(desc, session_id_,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000345 rtc::ToString(session_version_++))) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000346 delete offer;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000347 PostCreateSessionDescriptionFailed(request.observer,
348 "Failed to initialize the offer.");
wu@webrtc.org91053e72013-08-10 07:18:04 +0000349 return;
350 }
Steve Antond5585ca2017-10-23 14:49:26 -0700351 if (pc_->local_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700352 for (const cricket::MediaDescriptionOptions& options :
353 request.options.media_description_options) {
354 if (!options.transport_options.ice_restart) {
Steve Antond5585ca2017-10-23 14:49:26 -0700355 CopyCandidatesFromSessionDescription(pc_->local_description(),
zhihuang1c378ed2017-08-17 14:10:50 -0700356 options.mid, offer);
deadbeef0ed85b22016-02-23 17:24:52 -0800357 }
358 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000359 }
360 PostCreateSessionDescriptionSucceeded(request.observer, offer);
361}
362
363void WebRtcSessionDescriptionFactory::InternalCreateAnswer(
364 CreateSessionDescriptionRequest request) {
Steve Antond5585ca2017-10-23 14:49:26 -0700365 if (pc_->remote_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700366 for (cricket::MediaDescriptionOptions& options :
367 request.options.media_description_options) {
deadbeef0ed85b22016-02-23 17:24:52 -0800368 // According to http://tools.ietf.org/html/rfc5245#section-9.2.1.1
369 // an answer should also contain new ICE ufrag and password if an offer
370 // has been received with new ufrag and password.
zhihuang1c378ed2017-08-17 14:10:50 -0700371 options.transport_options.ice_restart =
Steve Antond5585ca2017-10-23 14:49:26 -0700372 pc_->IceRestartPending(options.mid);
deadbeef0ed85b22016-02-23 17:24:52 -0800373 // We should pass the current SSL role to the transport description
374 // factory, if there is already an existing ongoing session.
375 rtc::SSLRole ssl_role;
Steve Antond5585ca2017-10-23 14:49:26 -0700376 if (pc_->GetSslRole(options.mid, &ssl_role)) {
zhihuang1c378ed2017-08-17 14:10:50 -0700377 options.transport_options.prefer_passive_role =
deadbeef0ed85b22016-02-23 17:24:52 -0800378 (rtc::SSL_SERVER == ssl_role);
379 }
380 }
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000381 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000382
383 cricket::SessionDescription* desc(session_desc_factory_.CreateAnswer(
Steve Antond5585ca2017-10-23 14:49:26 -0700384 pc_->remote_description() ? pc_->remote_description()->description()
385 : nullptr,
386 request.options,
387 pc_->local_description() ? pc_->local_description()->description()
388 : nullptr));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000389 // RFC 3264
390 // If the answer is different from the offer in any way (different IP
391 // addresses, ports, etc.), the origin line MUST be different in the answer.
392 // In that case, the version number in the "o=" line of the answer is
393 // unrelated to the version number in the o line of the offer.
394 // Get a new version number by increasing the |session_version_answer_|.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200395 // The |session_version_| is a uint64_t, the wrap around should not happen.
nisseede5da42017-01-12 05:15:36 -0800396 RTC_DCHECK(session_version_ + 1 > session_version_);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000397 JsepSessionDescription* answer(new JsepSessionDescription(
398 JsepSessionDescription::kAnswer));
399 if (!answer->Initialize(desc, session_id_,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000400 rtc::ToString(session_version_++))) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000401 delete answer;
402 PostCreateSessionDescriptionFailed(request.observer,
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000403 "Failed to initialize the answer.");
wu@webrtc.org91053e72013-08-10 07:18:04 +0000404 return;
405 }
Steve Antond5585ca2017-10-23 14:49:26 -0700406 if (pc_->local_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700407 // Include all local ICE candidates in the SessionDescription unless
408 // the remote peer has requested an ICE restart.
409 for (const cricket::MediaDescriptionOptions& options :
410 request.options.media_description_options) {
411 if (!options.transport_options.ice_restart) {
Steve Antond5585ca2017-10-23 14:49:26 -0700412 CopyCandidatesFromSessionDescription(pc_->local_description(),
zhihuang1c378ed2017-08-17 14:10:50 -0700413 options.mid, answer);
deadbeef0ed85b22016-02-23 17:24:52 -0800414 }
415 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000416 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000417 PostCreateSessionDescriptionSucceeded(request.observer, answer);
418}
419
tommi0f620f42015-07-09 03:25:02 -0700420void WebRtcSessionDescriptionFactory::FailPendingRequests(
421 const std::string& reason) {
nisseede5da42017-01-12 05:15:36 -0800422 RTC_DCHECK(signaling_thread_->IsCurrent());
tommi0f620f42015-07-09 03:25:02 -0700423 while (!create_session_description_requests_.empty()) {
424 const CreateSessionDescriptionRequest& request =
425 create_session_description_requests_.front();
426 PostCreateSessionDescriptionFailed(request.observer,
427 ((request.type == CreateSessionDescriptionRequest::kOffer) ?
428 "CreateOffer" : "CreateAnswer") + reason);
429 create_session_description_requests_.pop();
430 }
431}
432
wu@webrtc.org91053e72013-08-10 07:18:04 +0000433void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionFailed(
434 CreateSessionDescriptionObserver* observer, const std::string& error) {
435 CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer);
436 msg->error = error;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700437 signaling_thread_->Post(RTC_FROM_HERE, this,
438 MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000439 LOG(LS_ERROR) << "Create SDP failed: " << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000440}
441
442void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionSucceeded(
443 CreateSessionDescriptionObserver* observer,
444 SessionDescriptionInterface* description) {
445 CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer);
446 msg->description.reset(description);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700447 signaling_thread_->Post(RTC_FROM_HERE, this,
448 MSG_CREATE_SESSIONDESCRIPTION_SUCCESS, msg);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000449}
450
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200451void WebRtcSessionDescriptionFactory::OnCertificateRequestFailed() {
nisseede5da42017-01-12 05:15:36 -0800452 RTC_DCHECK(signaling_thread_->IsCurrent());
wu@webrtc.org91053e72013-08-10 07:18:04 +0000453
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200454 LOG(LS_ERROR) << "Asynchronous certificate generation request failed.";
Henrik Boström87713d02015-08-25 09:53:21 +0200455 certificate_request_state_ = CERTIFICATE_FAILED;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000456
tommi0f620f42015-07-09 03:25:02 -0700457 FailPendingRequests(kFailedDueToIdentityFailed);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000458}
459
Henrik Boströmd8281982015-08-27 10:12:24 +0200460void WebRtcSessionDescriptionFactory::SetCertificate(
461 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
henrikg91d6ede2015-09-17 00:24:34 -0700462 RTC_DCHECK(certificate);
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200463 LOG(LS_VERBOSE) << "Setting new certificate.";
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000464
Henrik Boström87713d02015-08-25 09:53:21 +0200465 certificate_request_state_ = CERTIFICATE_SUCCEEDED;
Henrik Boströmd8281982015-08-27 10:12:24 +0200466 SignalCertificateReady(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000467
Henrik Boström3a14bf32015-08-31 09:27:58 +0200468 transport_desc_factory_.set_certificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000469 transport_desc_factory_.set_secure(cricket::SEC_ENABLED);
470
471 while (!create_session_description_requests_.empty()) {
472 if (create_session_description_requests_.front().type ==
473 CreateSessionDescriptionRequest::kOffer) {
474 InternalCreateOffer(create_session_description_requests_.front());
475 } else {
476 InternalCreateAnswer(create_session_description_requests_.front());
477 }
478 create_session_description_requests_.pop();
479 }
480}
wu@webrtc.org91053e72013-08-10 07:18:04 +0000481} // namespace webrtc