blob: 69c629e9df7ef91f506b377c1d1c4d4f9fd7dc73 [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
Steve Anton36b29d12017-10-30 09:57:42 -070013#include <algorithm>
14#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080015#include <utility>
Steve Anton36b29d12017-10-30 09:57:42 -070016#include <vector>
kwiberg0eb15ed2015-12-17 03:04:15 -080017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/jsep.h"
19#include "api/jsepsessiondescription.h"
20#include "api/mediaconstraintsinterface.h"
Steve Antond5585ca2017-10-23 14:49:26 -070021#include "pc/peerconnection.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/checks.h"
23#include "rtc_base/sslidentity.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000024
wu@webrtc.org364f2042013-11-20 21:49:41 +000025using cricket::MediaSessionOptions;
26
wu@webrtc.org91053e72013-08-10 07:18:04 +000027namespace webrtc {
wu@webrtc.org91053e72013-08-10 07:18:04 +000028namespace {
wu@webrtc.org91053e72013-08-10 07:18:04 +000029static const char kFailedDueToIdentityFailed[] =
30 " failed because DTLS identity request failed";
tommi0f620f42015-07-09 03:25:02 -070031static const char kFailedDueToSessionShutdown[] =
32 " failed because the session was shut down";
wu@webrtc.org91053e72013-08-10 07:18:04 +000033
Peter Boström0c4e06b2015-10-07 12:23:21 +020034static const uint64_t kInitSessionVersion = 2;
wu@webrtc.org91053e72013-08-10 07:18:04 +000035
zhihuang1c378ed2017-08-17 14:10:50 -070036static bool CompareSenderOptions(const cricket::SenderOptions& sender1,
37 const cricket::SenderOptions& sender2) {
38 return sender1.track_id < sender2.track_id;
wu@webrtc.org91053e72013-08-10 07:18:04 +000039}
40
zhihuang1c378ed2017-08-17 14:10:50 -070041static bool SameId(const cricket::SenderOptions& sender1,
42 const cricket::SenderOptions& sender2) {
43 return sender1.track_id == sender2.track_id;
wu@webrtc.org91053e72013-08-10 07:18:04 +000044}
45
zhihuang1c378ed2017-08-17 14:10:50 -070046// Check that each sender has a unique ID.
47static bool ValidMediaSessionOptions(
48 const cricket::MediaSessionOptions& session_options) {
49 std::vector<cricket::SenderOptions> sorted_senders;
50 for (const cricket::MediaDescriptionOptions& media_description_options :
51 session_options.media_description_options) {
52 sorted_senders.insert(sorted_senders.end(),
53 media_description_options.sender_options.begin(),
54 media_description_options.sender_options.end());
55 }
56 std::sort(sorted_senders.begin(), sorted_senders.end(), CompareSenderOptions);
57 std::vector<cricket::SenderOptions>::iterator it =
58 std::adjacent_find(sorted_senders.begin(), sorted_senders.end(), SameId);
59 return it == sorted_senders.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +000060}
61
62enum {
63 MSG_CREATE_SESSIONDESCRIPTION_SUCCESS,
Henrik Boström87713d02015-08-25 09:53:21 +020064 MSG_CREATE_SESSIONDESCRIPTION_FAILED,
65 MSG_USE_CONSTRUCTOR_CERTIFICATE
wu@webrtc.org91053e72013-08-10 07:18:04 +000066};
67
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000068struct CreateSessionDescriptionMsg : public rtc::MessageData {
wu@webrtc.org91053e72013-08-10 07:18:04 +000069 explicit CreateSessionDescriptionMsg(
70 webrtc::CreateSessionDescriptionObserver* observer)
71 : observer(observer) {
72 }
73
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000074 rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer;
wu@webrtc.org91053e72013-08-10 07:18:04 +000075 std::string error;
kwibergd1fe2812016-04-27 06:47:29 -070076 std::unique_ptr<webrtc::SessionDescriptionInterface> description;
wu@webrtc.org91053e72013-08-10 07:18:04 +000077};
wu@webrtc.org91053e72013-08-10 07:18:04 +000078} // namespace
79
Henrik Boströmd03c23b2016-06-01 11:44:18 +020080void WebRtcCertificateGeneratorCallback::OnFailure() {
81 SignalRequestFailed();
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000082}
83
Henrik Boströmd03c23b2016-06-01 11:44:18 +020084void WebRtcCertificateGeneratorCallback::OnSuccess(
85 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
86 SignalCertificateReady(certificate);
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000087}
88
wu@webrtc.org91053e72013-08-10 07:18:04 +000089// static
90void WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
91 const SessionDescriptionInterface* source_desc,
deadbeef0ed85b22016-02-23 17:24:52 -080092 const std::string& content_name,
wu@webrtc.org91053e72013-08-10 07:18:04 +000093 SessionDescriptionInterface* dest_desc) {
deadbeef0ed85b22016-02-23 17:24:52 -080094 if (!source_desc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +000095 return;
deadbeef0ed85b22016-02-23 17:24:52 -080096 }
97 const cricket::ContentInfos& contents =
98 source_desc->description()->contents();
99 const cricket::ContentInfo* cinfo =
100 source_desc->description()->GetContentByName(content_name);
101 if (!cinfo) {
102 return;
103 }
104 size_t mediasection_index = static_cast<int>(cinfo - &contents[0]);
105 const IceCandidateCollection* source_candidates =
106 source_desc->candidates(mediasection_index);
107 const IceCandidateCollection* dest_candidates =
108 dest_desc->candidates(mediasection_index);
Taylor Brandstetter4eb1ddd2016-03-01 16:21:07 -0800109 if (!source_candidates || !dest_candidates) {
110 return;
111 }
deadbeef0ed85b22016-02-23 17:24:52 -0800112 for (size_t n = 0; n < source_candidates->count(); ++n) {
113 const IceCandidateInterface* new_candidate = source_candidates->at(n);
114 if (!dest_candidates->HasCandidate(new_candidate)) {
115 dest_desc->AddCandidate(source_candidates->at(n));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000116 }
117 }
118}
119
Henrik Boström87713d02015-08-25 09:53:21 +0200120// Private constructor called by other constructors.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000121WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000122 rtc::Thread* signaling_thread,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000123 cricket::ChannelManager* channel_manager,
Steve Antond5585ca2017-10-23 14:49:26 -0700124 PeerConnection* pc,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000125 const std::string& session_id,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200126 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
127 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate)
wu@webrtc.org91053e72013-08-10 07:18:04 +0000128 : signaling_thread_(signaling_thread),
wu@webrtc.org91053e72013-08-10 07:18:04 +0000129 session_desc_factory_(channel_manager, &transport_desc_factory_),
130 // RFC 4566 suggested a Network Time Protocol (NTP) format timestamp
131 // as the session id and session version. To simplify, it should be fine
132 // to just use a random number as session id and start version from
133 // |kInitSessionVersion|.
134 session_version_(kInitSessionVersion),
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200135 cert_generator_(std::move(cert_generator)),
Steve Antond5585ca2017-10-23 14:49:26 -0700136 pc_(pc),
wu@webrtc.org91053e72013-08-10 07:18:04 +0000137 session_id_(session_id),
Henrik Boström87713d02015-08-25 09:53:21 +0200138 certificate_request_state_(CERTIFICATE_NOT_NEEDED) {
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200139 RTC_DCHECK(signaling_thread_);
Steve Antond5585ca2017-10-23 14:49:26 -0700140 RTC_DCHECK(!(cert_generator_ && certificate));
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200141 bool dtls_enabled = cert_generator_ || certificate;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000142 // SRTP-SDES is disabled if DTLS is on.
143 SetSdesPolicy(dtls_enabled ? cricket::SEC_DISABLED : cricket::SEC_REQUIRED);
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200144 if (!dtls_enabled) {
145 LOG(LS_VERBOSE) << "DTLS-SRTP disabled.";
146 return;
147 }
148
149 if (certificate) {
150 // Use |certificate|.
151 certificate_request_state_ = CERTIFICATE_WAITING;
152
153 LOG(LS_VERBOSE) << "DTLS-SRTP enabled; has certificate parameter.";
154 // We already have a certificate but we wait to do |SetIdentity|; if we do
155 // it in the constructor then the caller has not had a chance to connect to
156 // |SignalCertificateReady|.
157 signaling_thread_->Post(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700158 RTC_FROM_HERE, this, MSG_USE_CONSTRUCTOR_CERTIFICATE,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200159 new rtc::ScopedRefMessageData<rtc::RTCCertificate>(certificate));
160 } else {
161 // Generate certificate.
162 certificate_request_state_ = CERTIFICATE_WAITING;
163
164 rtc::scoped_refptr<WebRtcCertificateGeneratorCallback> callback(
165 new rtc::RefCountedObject<WebRtcCertificateGeneratorCallback>());
166 callback->SignalRequestFailed.connect(
167 this, &WebRtcSessionDescriptionFactory::OnCertificateRequestFailed);
168 callback->SignalCertificateReady.connect(
169 this, &WebRtcSessionDescriptionFactory::SetCertificate);
170
171 rtc::KeyParams key_params = rtc::KeyParams();
172 LOG(LS_VERBOSE) << "DTLS-SRTP enabled; sending DTLS identity request (key "
173 << "type: " << key_params.type() << ").";
174
175 // Request certificate. This happens asynchronously, so that the caller gets
176 // a chance to connect to |SignalCertificateReady|.
177 cert_generator_->GenerateCertificateAsync(
178 key_params, rtc::Optional<uint64_t>(), callback);
179 }
Henrik Boström87713d02015-08-25 09:53:21 +0200180}
wu@webrtc.org91053e72013-08-10 07:18:04 +0000181
wu@webrtc.org91053e72013-08-10 07:18:04 +0000182WebRtcSessionDescriptionFactory::~WebRtcSessionDescriptionFactory() {
nisseede5da42017-01-12 05:15:36 -0800183 RTC_DCHECK(signaling_thread_->IsCurrent());
tommi0f620f42015-07-09 03:25:02 -0700184
185 // Fail any requests that were asked for before identity generation completed.
186 FailPendingRequests(kFailedDueToSessionShutdown);
187
188 // Process all pending notifications in the message queue. If we don't do
189 // this, requests will linger and not know they succeeded or failed.
190 rtc::MessageList list;
191 signaling_thread_->Clear(this, rtc::MQID_ANY, &list);
Henrik Boström87713d02015-08-25 09:53:21 +0200192 for (auto& msg : list) {
193 if (msg.message_id != MSG_USE_CONSTRUCTOR_CERTIFICATE) {
194 OnMessage(&msg);
195 } else {
196 // Skip MSG_USE_CONSTRUCTOR_CERTIFICATE because we don't want to trigger
197 // SetIdentity-related callbacks in the destructor. This can be a problem
198 // when WebRtcSession listens to the callback but it was the WebRtcSession
199 // destructor that caused WebRtcSessionDescriptionFactory's destruction.
200 // The callback is then ignored, leaking memory allocated by OnMessage for
201 // MSG_USE_CONSTRUCTOR_CERTIFICATE.
202 delete msg.pdata;
203 }
204 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000205}
206
207void WebRtcSessionDescriptionFactory::CreateOffer(
208 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700209 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
210 const cricket::MediaSessionOptions& session_options) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000211 std::string error = "CreateOffer";
Henrik Boström87713d02015-08-25 09:53:21 +0200212 if (certificate_request_state_ == CERTIFICATE_FAILED) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000213 error += kFailedDueToIdentityFailed;
214 LOG(LS_ERROR) << error;
215 PostCreateSessionDescriptionFailed(observer, error);
216 return;
217 }
218
zhihuang1c378ed2017-08-17 14:10:50 -0700219 if (!ValidMediaSessionOptions(session_options)) {
220 error += " called with invalid session options";
wu@webrtc.org91053e72013-08-10 07:18:04 +0000221 LOG(LS_ERROR) << error;
222 PostCreateSessionDescriptionFailed(observer, error);
223 return;
224 }
225
wu@webrtc.org91053e72013-08-10 07:18:04 +0000226 CreateSessionDescriptionRequest request(
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000227 CreateSessionDescriptionRequest::kOffer, observer, session_options);
Henrik Boström87713d02015-08-25 09:53:21 +0200228 if (certificate_request_state_ == CERTIFICATE_WAITING) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000229 create_session_description_requests_.push(request);
230 } else {
nisseede5da42017-01-12 05:15:36 -0800231 RTC_DCHECK(certificate_request_state_ == CERTIFICATE_SUCCEEDED ||
232 certificate_request_state_ == CERTIFICATE_NOT_NEEDED);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000233 InternalCreateOffer(request);
234 }
235}
236
237void WebRtcSessionDescriptionFactory::CreateAnswer(
238 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700239 const cricket::MediaSessionOptions& session_options) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000240 std::string error = "CreateAnswer";
Henrik Boström87713d02015-08-25 09:53:21 +0200241 if (certificate_request_state_ == CERTIFICATE_FAILED) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000242 error += kFailedDueToIdentityFailed;
243 LOG(LS_ERROR) << error;
244 PostCreateSessionDescriptionFailed(observer, error);
245 return;
246 }
Steve Antond5585ca2017-10-23 14:49:26 -0700247 if (!pc_->remote_description()) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000248 error += " can't be called before SetRemoteDescription.";
249 LOG(LS_ERROR) << error;
250 PostCreateSessionDescriptionFailed(observer, error);
251 return;
252 }
Steve Antond5585ca2017-10-23 14:49:26 -0700253 if (pc_->remote_description()->type() != JsepSessionDescription::kOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000254 error += " failed because remote_description is not an offer.";
255 LOG(LS_ERROR) << error;
256 PostCreateSessionDescriptionFailed(observer, error);
257 return;
258 }
259
zhihuang1c378ed2017-08-17 14:10:50 -0700260 if (!ValidMediaSessionOptions(session_options)) {
261 error += " called with invalid session options.";
wu@webrtc.org91053e72013-08-10 07:18:04 +0000262 LOG(LS_ERROR) << error;
263 PostCreateSessionDescriptionFailed(observer, error);
264 return;
265 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000266
267 CreateSessionDescriptionRequest request(
deadbeefab9b2d12015-10-14 11:33:11 -0700268 CreateSessionDescriptionRequest::kAnswer, observer, session_options);
Henrik Boström87713d02015-08-25 09:53:21 +0200269 if (certificate_request_state_ == CERTIFICATE_WAITING) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000270 create_session_description_requests_.push(request);
271 } else {
nisseede5da42017-01-12 05:15:36 -0800272 RTC_DCHECK(certificate_request_state_ == CERTIFICATE_SUCCEEDED ||
273 certificate_request_state_ == CERTIFICATE_NOT_NEEDED);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000274 InternalCreateAnswer(request);
275 }
276}
277
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000278void WebRtcSessionDescriptionFactory::SetSdesPolicy(
279 cricket::SecurePolicy secure_policy) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000280 session_desc_factory_.set_secure(secure_policy);
281}
282
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000283cricket::SecurePolicy WebRtcSessionDescriptionFactory::SdesPolicy() const {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000284 return session_desc_factory_.secure();
285}
286
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000287void WebRtcSessionDescriptionFactory::OnMessage(rtc::Message* msg) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000288 switch (msg->message_id) {
289 case MSG_CREATE_SESSIONDESCRIPTION_SUCCESS: {
290 CreateSessionDescriptionMsg* param =
291 static_cast<CreateSessionDescriptionMsg*>(msg->pdata);
292 param->observer->OnSuccess(param->description.release());
293 delete param;
294 break;
295 }
296 case MSG_CREATE_SESSIONDESCRIPTION_FAILED: {
297 CreateSessionDescriptionMsg* param =
298 static_cast<CreateSessionDescriptionMsg*>(msg->pdata);
299 param->observer->OnFailure(param->error);
300 delete param;
301 break;
302 }
Henrik Boström87713d02015-08-25 09:53:21 +0200303 case MSG_USE_CONSTRUCTOR_CERTIFICATE: {
304 rtc::ScopedRefMessageData<rtc::RTCCertificate>* param =
305 static_cast<rtc::ScopedRefMessageData<rtc::RTCCertificate>*>(
306 msg->pdata);
307 LOG(LS_INFO) << "Using certificate supplied to the constructor.";
Henrik Boströmd8281982015-08-27 10:12:24 +0200308 SetCertificate(param->data());
Henrik Boström87713d02015-08-25 09:53:21 +0200309 delete param;
310 break;
311 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000312 default:
nissec80e7412017-01-11 05:56:46 -0800313 RTC_NOTREACHED();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000314 break;
315 }
316}
317
318void WebRtcSessionDescriptionFactory::InternalCreateOffer(
319 CreateSessionDescriptionRequest request) {
Steve Antond5585ca2017-10-23 14:49:26 -0700320 if (pc_->local_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700321 // If the needs-ice-restart flag is set as described by JSEP, we should
322 // generate an offer with a new ufrag/password to trigger an ICE restart.
323 for (cricket::MediaDescriptionOptions& options :
324 request.options.media_description_options) {
Steve Antond5585ca2017-10-23 14:49:26 -0700325 if (pc_->NeedsIceRestart(options.mid)) {
zhihuang1c378ed2017-08-17 14:10:50 -0700326 options.transport_options.ice_restart = true;
deadbeefd1a38b52016-12-10 13:15:33 -0800327 }
328 }
329 }
330
deadbeefd59daf82015-10-14 15:02:44 -0700331 cricket::SessionDescription* desc(session_desc_factory_.CreateOffer(
Steve Antond5585ca2017-10-23 14:49:26 -0700332 request.options, pc_->local_description()
333 ? pc_->local_description()->description()
deadbeefd59daf82015-10-14 15:02:44 -0700334 : nullptr));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000335 // RFC 3264
336 // When issuing an offer that modifies the session,
337 // the "o=" line of the new SDP MUST be identical to that in the
338 // previous SDP, except that the version in the origin field MUST
339 // increment by one from the previous SDP.
340
341 // Just increase the version number by one each time when a new offer
342 // is created regardless if it's identical to the previous one or not.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200343 // The |session_version_| is a uint64_t, the wrap around should not happen.
nisseede5da42017-01-12 05:15:36 -0800344 RTC_DCHECK(session_version_ + 1 > session_version_);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000345 JsepSessionDescription* offer(new JsepSessionDescription(
346 JsepSessionDescription::kOffer));
347 if (!offer->Initialize(desc, session_id_,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000348 rtc::ToString(session_version_++))) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000349 delete offer;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000350 PostCreateSessionDescriptionFailed(request.observer,
351 "Failed to initialize the offer.");
wu@webrtc.org91053e72013-08-10 07:18:04 +0000352 return;
353 }
Steve Antond5585ca2017-10-23 14:49:26 -0700354 if (pc_->local_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700355 for (const cricket::MediaDescriptionOptions& options :
356 request.options.media_description_options) {
357 if (!options.transport_options.ice_restart) {
Steve Antond5585ca2017-10-23 14:49:26 -0700358 CopyCandidatesFromSessionDescription(pc_->local_description(),
zhihuang1c378ed2017-08-17 14:10:50 -0700359 options.mid, offer);
deadbeef0ed85b22016-02-23 17:24:52 -0800360 }
361 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000362 }
363 PostCreateSessionDescriptionSucceeded(request.observer, offer);
364}
365
366void WebRtcSessionDescriptionFactory::InternalCreateAnswer(
367 CreateSessionDescriptionRequest request) {
Steve Antond5585ca2017-10-23 14:49:26 -0700368 if (pc_->remote_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700369 for (cricket::MediaDescriptionOptions& options :
370 request.options.media_description_options) {
deadbeef0ed85b22016-02-23 17:24:52 -0800371 // According to http://tools.ietf.org/html/rfc5245#section-9.2.1.1
372 // an answer should also contain new ICE ufrag and password if an offer
373 // has been received with new ufrag and password.
zhihuang1c378ed2017-08-17 14:10:50 -0700374 options.transport_options.ice_restart =
Steve Antond5585ca2017-10-23 14:49:26 -0700375 pc_->IceRestartPending(options.mid);
deadbeef0ed85b22016-02-23 17:24:52 -0800376 // We should pass the current SSL role to the transport description
377 // factory, if there is already an existing ongoing session.
378 rtc::SSLRole ssl_role;
Steve Antond5585ca2017-10-23 14:49:26 -0700379 if (pc_->GetSslRole(options.mid, &ssl_role)) {
zhihuang1c378ed2017-08-17 14:10:50 -0700380 options.transport_options.prefer_passive_role =
deadbeef0ed85b22016-02-23 17:24:52 -0800381 (rtc::SSL_SERVER == ssl_role);
382 }
383 }
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000384 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000385
386 cricket::SessionDescription* desc(session_desc_factory_.CreateAnswer(
Steve Antond5585ca2017-10-23 14:49:26 -0700387 pc_->remote_description() ? pc_->remote_description()->description()
388 : nullptr,
389 request.options,
390 pc_->local_description() ? pc_->local_description()->description()
391 : nullptr));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000392 // RFC 3264
393 // If the answer is different from the offer in any way (different IP
394 // addresses, ports, etc.), the origin line MUST be different in the answer.
395 // In that case, the version number in the "o=" line of the answer is
396 // unrelated to the version number in the o line of the offer.
397 // Get a new version number by increasing the |session_version_answer_|.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200398 // The |session_version_| is a uint64_t, the wrap around should not happen.
nisseede5da42017-01-12 05:15:36 -0800399 RTC_DCHECK(session_version_ + 1 > session_version_);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000400 JsepSessionDescription* answer(new JsepSessionDescription(
401 JsepSessionDescription::kAnswer));
402 if (!answer->Initialize(desc, session_id_,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000403 rtc::ToString(session_version_++))) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000404 delete answer;
405 PostCreateSessionDescriptionFailed(request.observer,
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000406 "Failed to initialize the answer.");
wu@webrtc.org91053e72013-08-10 07:18:04 +0000407 return;
408 }
Steve Antond5585ca2017-10-23 14:49:26 -0700409 if (pc_->local_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700410 // Include all local ICE candidates in the SessionDescription unless
411 // the remote peer has requested an ICE restart.
412 for (const cricket::MediaDescriptionOptions& options :
413 request.options.media_description_options) {
414 if (!options.transport_options.ice_restart) {
Steve Antond5585ca2017-10-23 14:49:26 -0700415 CopyCandidatesFromSessionDescription(pc_->local_description(),
zhihuang1c378ed2017-08-17 14:10:50 -0700416 options.mid, answer);
deadbeef0ed85b22016-02-23 17:24:52 -0800417 }
418 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000419 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000420 PostCreateSessionDescriptionSucceeded(request.observer, answer);
421}
422
tommi0f620f42015-07-09 03:25:02 -0700423void WebRtcSessionDescriptionFactory::FailPendingRequests(
424 const std::string& reason) {
nisseede5da42017-01-12 05:15:36 -0800425 RTC_DCHECK(signaling_thread_->IsCurrent());
tommi0f620f42015-07-09 03:25:02 -0700426 while (!create_session_description_requests_.empty()) {
427 const CreateSessionDescriptionRequest& request =
428 create_session_description_requests_.front();
429 PostCreateSessionDescriptionFailed(request.observer,
430 ((request.type == CreateSessionDescriptionRequest::kOffer) ?
431 "CreateOffer" : "CreateAnswer") + reason);
432 create_session_description_requests_.pop();
433 }
434}
435
wu@webrtc.org91053e72013-08-10 07:18:04 +0000436void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionFailed(
437 CreateSessionDescriptionObserver* observer, const std::string& error) {
438 CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer);
439 msg->error = error;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700440 signaling_thread_->Post(RTC_FROM_HERE, this,
441 MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000442 LOG(LS_ERROR) << "Create SDP failed: " << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000443}
444
445void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionSucceeded(
446 CreateSessionDescriptionObserver* observer,
447 SessionDescriptionInterface* description) {
448 CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer);
449 msg->description.reset(description);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700450 signaling_thread_->Post(RTC_FROM_HERE, this,
451 MSG_CREATE_SESSIONDESCRIPTION_SUCCESS, msg);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000452}
453
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200454void WebRtcSessionDescriptionFactory::OnCertificateRequestFailed() {
nisseede5da42017-01-12 05:15:36 -0800455 RTC_DCHECK(signaling_thread_->IsCurrent());
wu@webrtc.org91053e72013-08-10 07:18:04 +0000456
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200457 LOG(LS_ERROR) << "Asynchronous certificate generation request failed.";
Henrik Boström87713d02015-08-25 09:53:21 +0200458 certificate_request_state_ = CERTIFICATE_FAILED;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000459
tommi0f620f42015-07-09 03:25:02 -0700460 FailPendingRequests(kFailedDueToIdentityFailed);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000461}
462
Henrik Boströmd8281982015-08-27 10:12:24 +0200463void WebRtcSessionDescriptionFactory::SetCertificate(
464 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
henrikg91d6ede2015-09-17 00:24:34 -0700465 RTC_DCHECK(certificate);
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200466 LOG(LS_VERBOSE) << "Setting new certificate.";
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000467
Henrik Boström87713d02015-08-25 09:53:21 +0200468 certificate_request_state_ = CERTIFICATE_SUCCEEDED;
Henrik Boströmd8281982015-08-27 10:12:24 +0200469 SignalCertificateReady(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000470
Henrik Boström3a14bf32015-08-31 09:27:58 +0200471 transport_desc_factory_.set_certificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000472 transport_desc_factory_.set_secure(cricket::SEC_ENABLED);
473
474 while (!create_session_description_requests_.empty()) {
475 if (create_session_description_requests_.front().type ==
476 CreateSessionDescriptionRequest::kOffer) {
477 InternalCreateOffer(create_session_description_requests_.front());
478 } else {
479 InternalCreateAnswer(create_session_description_requests_.front());
480 }
481 create_session_description_requests_.pop();
482 }
483}
wu@webrtc.org91053e72013-08-10 07:18:04 +0000484} // namespace webrtc