blob: d859559f48697b8dcddd8f9acd7728fa54d4972e [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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "pc/webrtc_session_description_factory.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <stddef.h>
Steve Anton36b29d12017-10-30 09:57:42 -070014#include <algorithm>
15#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080016#include <utility>
Steve Anton36b29d12017-10-30 09:57:42 -070017#include <vector>
kwiberg0eb15ed2015-12-17 03:04:15 -080018
Karl Wiberg918f50c2018-07-05 11:40:33 +020019#include "absl/memory/memory.h"
Yves Gerey3e707812018-11-28 16:47:49 +010020#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "api/jsep.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "api/jsep_session_description.h"
23#include "api/rtc_error.h"
24#include "pc/session_description.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/checks.h"
Yves Gerey3e707812018-11-28 16:47:49 +010026#include "rtc_base/location.h"
27#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "rtc_base/ref_counted_object.h"
29#include "rtc_base/ssl_identity.h"
30#include "rtc_base/ssl_stream_adapter.h"
31#include "rtc_base/string_encode.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000032
wu@webrtc.org364f2042013-11-20 21:49:41 +000033using cricket::MediaSessionOptions;
Amit Hilbuchbcd39d42019-01-25 17:13:56 -080034using rtc::UniqueRandomIdGenerator;
wu@webrtc.org364f2042013-11-20 21:49:41 +000035
wu@webrtc.org91053e72013-08-10 07:18:04 +000036namespace webrtc {
wu@webrtc.org91053e72013-08-10 07:18:04 +000037namespace {
wu@webrtc.org91053e72013-08-10 07:18:04 +000038static const char kFailedDueToIdentityFailed[] =
39 " failed because DTLS identity request failed";
tommi0f620f42015-07-09 03:25:02 -070040static const char kFailedDueToSessionShutdown[] =
41 " failed because the session was shut down";
wu@webrtc.org91053e72013-08-10 07:18:04 +000042
Peter Boström0c4e06b2015-10-07 12:23:21 +020043static const uint64_t kInitSessionVersion = 2;
wu@webrtc.org91053e72013-08-10 07:18:04 +000044
zhihuang1c378ed2017-08-17 14:10:50 -070045static bool CompareSenderOptions(const cricket::SenderOptions& sender1,
46 const cricket::SenderOptions& sender2) {
47 return sender1.track_id < sender2.track_id;
wu@webrtc.org91053e72013-08-10 07:18:04 +000048}
49
zhihuang1c378ed2017-08-17 14:10:50 -070050static bool SameId(const cricket::SenderOptions& sender1,
51 const cricket::SenderOptions& sender2) {
52 return sender1.track_id == sender2.track_id;
wu@webrtc.org91053e72013-08-10 07:18:04 +000053}
54
zhihuang1c378ed2017-08-17 14:10:50 -070055// Check that each sender has a unique ID.
56static bool ValidMediaSessionOptions(
57 const cricket::MediaSessionOptions& session_options) {
58 std::vector<cricket::SenderOptions> sorted_senders;
59 for (const cricket::MediaDescriptionOptions& media_description_options :
60 session_options.media_description_options) {
61 sorted_senders.insert(sorted_senders.end(),
62 media_description_options.sender_options.begin(),
63 media_description_options.sender_options.end());
64 }
65 std::sort(sorted_senders.begin(), sorted_senders.end(), CompareSenderOptions);
66 std::vector<cricket::SenderOptions>::iterator it =
67 std::adjacent_find(sorted_senders.begin(), sorted_senders.end(), SameId);
68 return it == sorted_senders.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +000069}
70
71enum {
72 MSG_CREATE_SESSIONDESCRIPTION_SUCCESS,
Henrik Boström87713d02015-08-25 09:53:21 +020073 MSG_CREATE_SESSIONDESCRIPTION_FAILED,
74 MSG_USE_CONSTRUCTOR_CERTIFICATE
wu@webrtc.org91053e72013-08-10 07:18:04 +000075};
76
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000077struct CreateSessionDescriptionMsg : public rtc::MessageData {
wu@webrtc.org91053e72013-08-10 07:18:04 +000078 explicit CreateSessionDescriptionMsg(
Harald Alvestrand73771a82018-05-24 10:53:49 +020079 webrtc::CreateSessionDescriptionObserver* observer,
80 RTCError error_in)
81 : observer(observer), error(std::move(error_in)) {}
wu@webrtc.org91053e72013-08-10 07:18:04 +000082
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000083 rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer;
Harald Alvestrand73771a82018-05-24 10:53:49 +020084 RTCError error;
kwibergd1fe2812016-04-27 06:47:29 -070085 std::unique_ptr<webrtc::SessionDescriptionInterface> description;
wu@webrtc.org91053e72013-08-10 07:18:04 +000086};
wu@webrtc.org91053e72013-08-10 07:18:04 +000087} // namespace
88
Henrik Boströmd03c23b2016-06-01 11:44:18 +020089void WebRtcCertificateGeneratorCallback::OnFailure() {
90 SignalRequestFailed();
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000091}
92
Henrik Boströmd03c23b2016-06-01 11:44:18 +020093void WebRtcCertificateGeneratorCallback::OnSuccess(
94 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
95 SignalCertificateReady(certificate);
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000096}
97
wu@webrtc.org91053e72013-08-10 07:18:04 +000098// static
99void WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
100 const SessionDescriptionInterface* source_desc,
deadbeef0ed85b22016-02-23 17:24:52 -0800101 const std::string& content_name,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000102 SessionDescriptionInterface* dest_desc) {
deadbeef0ed85b22016-02-23 17:24:52 -0800103 if (!source_desc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000104 return;
deadbeef0ed85b22016-02-23 17:24:52 -0800105 }
106 const cricket::ContentInfos& contents =
107 source_desc->description()->contents();
108 const cricket::ContentInfo* cinfo =
109 source_desc->description()->GetContentByName(content_name);
110 if (!cinfo) {
111 return;
112 }
113 size_t mediasection_index = static_cast<int>(cinfo - &contents[0]);
114 const IceCandidateCollection* source_candidates =
115 source_desc->candidates(mediasection_index);
116 const IceCandidateCollection* dest_candidates =
117 dest_desc->candidates(mediasection_index);
Taylor Brandstetter4eb1ddd2016-03-01 16:21:07 -0800118 if (!source_candidates || !dest_candidates) {
119 return;
120 }
deadbeef0ed85b22016-02-23 17:24:52 -0800121 for (size_t n = 0; n < source_candidates->count(); ++n) {
122 const IceCandidateInterface* new_candidate = source_candidates->at(n);
123 if (!dest_candidates->HasCandidate(new_candidate)) {
124 dest_desc->AddCandidate(source_candidates->at(n));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000125 }
126 }
127}
128
129WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000130 rtc::Thread* signaling_thread,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000131 cricket::ChannelManager* channel_manager,
Steve Anton2d8609c2018-01-23 16:38:46 -0800132 PeerConnectionInternal* pc,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000133 const std::string& session_id,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200134 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
Amit Hilbuchbcd39d42019-01-25 17:13:56 -0800135 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate,
136 UniqueRandomIdGenerator* ssrc_generator)
wu@webrtc.org91053e72013-08-10 07:18:04 +0000137 : signaling_thread_(signaling_thread),
Amit Hilbuchbcd39d42019-01-25 17:13:56 -0800138 session_desc_factory_(channel_manager,
139 &transport_desc_factory_,
140 ssrc_generator),
wu@webrtc.org91053e72013-08-10 07:18:04 +0000141 // RFC 4566 suggested a Network Time Protocol (NTP) format timestamp
142 // as the session id and session version. To simplify, it should be fine
143 // to just use a random number as session id and start version from
144 // |kInitSessionVersion|.
145 session_version_(kInitSessionVersion),
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200146 cert_generator_(std::move(cert_generator)),
Steve Antond5585ca2017-10-23 14:49:26 -0700147 pc_(pc),
wu@webrtc.org91053e72013-08-10 07:18:04 +0000148 session_id_(session_id),
Henrik Boström87713d02015-08-25 09:53:21 +0200149 certificate_request_state_(CERTIFICATE_NOT_NEEDED) {
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200150 RTC_DCHECK(signaling_thread_);
Steve Antond5585ca2017-10-23 14:49:26 -0700151 RTC_DCHECK(!(cert_generator_ && certificate));
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200152 bool dtls_enabled = cert_generator_ || certificate;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000153 // SRTP-SDES is disabled if DTLS is on.
154 SetSdesPolicy(dtls_enabled ? cricket::SEC_DISABLED : cricket::SEC_REQUIRED);
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200155 if (!dtls_enabled) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100156 RTC_LOG(LS_VERBOSE) << "DTLS-SRTP disabled.";
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200157 return;
158 }
159
160 if (certificate) {
161 // Use |certificate|.
162 certificate_request_state_ = CERTIFICATE_WAITING;
163
Mirko Bonadei675513b2017-11-09 11:09:25 +0100164 RTC_LOG(LS_VERBOSE) << "DTLS-SRTP enabled; has certificate parameter.";
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200165 // We already have a certificate but we wait to do |SetIdentity|; if we do
166 // it in the constructor then the caller has not had a chance to connect to
167 // |SignalCertificateReady|.
168 signaling_thread_->Post(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700169 RTC_FROM_HERE, this, MSG_USE_CONSTRUCTOR_CERTIFICATE,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200170 new rtc::ScopedRefMessageData<rtc::RTCCertificate>(certificate));
171 } else {
172 // Generate certificate.
173 certificate_request_state_ = CERTIFICATE_WAITING;
174
175 rtc::scoped_refptr<WebRtcCertificateGeneratorCallback> callback(
176 new rtc::RefCountedObject<WebRtcCertificateGeneratorCallback>());
177 callback->SignalRequestFailed.connect(
178 this, &WebRtcSessionDescriptionFactory::OnCertificateRequestFailed);
179 callback->SignalCertificateReady.connect(
180 this, &WebRtcSessionDescriptionFactory::SetCertificate);
181
182 rtc::KeyParams key_params = rtc::KeyParams();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100183 RTC_LOG(LS_VERBOSE)
Jonas Olsson45cc8902018-02-13 10:37:07 +0100184 << "DTLS-SRTP enabled; sending DTLS identity request (key type: "
185 << key_params.type() << ").";
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200186
187 // Request certificate. This happens asynchronously, so that the caller gets
188 // a chance to connect to |SignalCertificateReady|.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200189 cert_generator_->GenerateCertificateAsync(key_params, absl::nullopt,
Oskar Sundbom36f8f3e2017-11-16 10:54:27 +0100190 callback);
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200191 }
Henrik Boström87713d02015-08-25 09:53:21 +0200192}
wu@webrtc.org91053e72013-08-10 07:18:04 +0000193
wu@webrtc.org91053e72013-08-10 07:18:04 +0000194WebRtcSessionDescriptionFactory::~WebRtcSessionDescriptionFactory() {
nisseede5da42017-01-12 05:15:36 -0800195 RTC_DCHECK(signaling_thread_->IsCurrent());
tommi0f620f42015-07-09 03:25:02 -0700196
197 // Fail any requests that were asked for before identity generation completed.
198 FailPendingRequests(kFailedDueToSessionShutdown);
199
200 // Process all pending notifications in the message queue. If we don't do
201 // this, requests will linger and not know they succeeded or failed.
202 rtc::MessageList list;
203 signaling_thread_->Clear(this, rtc::MQID_ANY, &list);
Henrik Boström87713d02015-08-25 09:53:21 +0200204 for (auto& msg : list) {
205 if (msg.message_id != MSG_USE_CONSTRUCTOR_CERTIFICATE) {
206 OnMessage(&msg);
207 } else {
208 // Skip MSG_USE_CONSTRUCTOR_CERTIFICATE because we don't want to trigger
209 // SetIdentity-related callbacks in the destructor. This can be a problem
210 // when WebRtcSession listens to the callback but it was the WebRtcSession
211 // destructor that caused WebRtcSessionDescriptionFactory's destruction.
212 // The callback is then ignored, leaking memory allocated by OnMessage for
213 // MSG_USE_CONSTRUCTOR_CERTIFICATE.
214 delete msg.pdata;
215 }
216 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000217}
218
219void WebRtcSessionDescriptionFactory::CreateOffer(
220 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700221 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
222 const cricket::MediaSessionOptions& session_options) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000223 std::string error = "CreateOffer";
Henrik Boström87713d02015-08-25 09:53:21 +0200224 if (certificate_request_state_ == CERTIFICATE_FAILED) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000225 error += kFailedDueToIdentityFailed;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100226 RTC_LOG(LS_ERROR) << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000227 PostCreateSessionDescriptionFailed(observer, error);
228 return;
229 }
230
zhihuang1c378ed2017-08-17 14:10:50 -0700231 if (!ValidMediaSessionOptions(session_options)) {
232 error += " called with invalid session options";
Mirko Bonadei675513b2017-11-09 11:09:25 +0100233 RTC_LOG(LS_ERROR) << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000234 PostCreateSessionDescriptionFailed(observer, error);
235 return;
236 }
237
wu@webrtc.org91053e72013-08-10 07:18:04 +0000238 CreateSessionDescriptionRequest request(
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000239 CreateSessionDescriptionRequest::kOffer, observer, session_options);
Henrik Boström87713d02015-08-25 09:53:21 +0200240 if (certificate_request_state_ == CERTIFICATE_WAITING) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000241 create_session_description_requests_.push(request);
242 } else {
nisseede5da42017-01-12 05:15:36 -0800243 RTC_DCHECK(certificate_request_state_ == CERTIFICATE_SUCCEEDED ||
244 certificate_request_state_ == CERTIFICATE_NOT_NEEDED);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000245 InternalCreateOffer(request);
246 }
247}
248
249void WebRtcSessionDescriptionFactory::CreateAnswer(
250 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700251 const cricket::MediaSessionOptions& session_options) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000252 std::string error = "CreateAnswer";
Henrik Boström87713d02015-08-25 09:53:21 +0200253 if (certificate_request_state_ == CERTIFICATE_FAILED) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000254 error += kFailedDueToIdentityFailed;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100255 RTC_LOG(LS_ERROR) << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000256 PostCreateSessionDescriptionFailed(observer, error);
257 return;
258 }
Steve Antond5585ca2017-10-23 14:49:26 -0700259 if (!pc_->remote_description()) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000260 error += " can't be called before SetRemoteDescription.";
Mirko Bonadei675513b2017-11-09 11:09:25 +0100261 RTC_LOG(LS_ERROR) << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000262 PostCreateSessionDescriptionFailed(observer, error);
263 return;
264 }
Steve Antona3a92c22017-12-07 10:27:41 -0800265 if (pc_->remote_description()->GetType() != SdpType::kOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000266 error += " failed because remote_description is not an offer.";
Mirko Bonadei675513b2017-11-09 11:09:25 +0100267 RTC_LOG(LS_ERROR) << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000268 PostCreateSessionDescriptionFailed(observer, error);
269 return;
270 }
271
zhihuang1c378ed2017-08-17 14:10:50 -0700272 if (!ValidMediaSessionOptions(session_options)) {
273 error += " called with invalid session options.";
Mirko Bonadei675513b2017-11-09 11:09:25 +0100274 RTC_LOG(LS_ERROR) << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000275 PostCreateSessionDescriptionFailed(observer, error);
276 return;
277 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000278
279 CreateSessionDescriptionRequest request(
deadbeefab9b2d12015-10-14 11:33:11 -0700280 CreateSessionDescriptionRequest::kAnswer, observer, session_options);
Henrik Boström87713d02015-08-25 09:53:21 +0200281 if (certificate_request_state_ == CERTIFICATE_WAITING) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000282 create_session_description_requests_.push(request);
283 } else {
nisseede5da42017-01-12 05:15:36 -0800284 RTC_DCHECK(certificate_request_state_ == CERTIFICATE_SUCCEEDED ||
285 certificate_request_state_ == CERTIFICATE_NOT_NEEDED);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000286 InternalCreateAnswer(request);
287 }
288}
289
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000290void WebRtcSessionDescriptionFactory::SetSdesPolicy(
291 cricket::SecurePolicy secure_policy) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000292 session_desc_factory_.set_secure(secure_policy);
293}
294
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000295cricket::SecurePolicy WebRtcSessionDescriptionFactory::SdesPolicy() const {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000296 return session_desc_factory_.secure();
297}
298
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000299void WebRtcSessionDescriptionFactory::OnMessage(rtc::Message* msg) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000300 switch (msg->message_id) {
301 case MSG_CREATE_SESSIONDESCRIPTION_SUCCESS: {
302 CreateSessionDescriptionMsg* param =
303 static_cast<CreateSessionDescriptionMsg*>(msg->pdata);
304 param->observer->OnSuccess(param->description.release());
305 delete param;
306 break;
307 }
308 case MSG_CREATE_SESSIONDESCRIPTION_FAILED: {
309 CreateSessionDescriptionMsg* param =
310 static_cast<CreateSessionDescriptionMsg*>(msg->pdata);
Harald Alvestrand73771a82018-05-24 10:53:49 +0200311 param->observer->OnFailure(std::move(param->error));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000312 delete param;
313 break;
314 }
Henrik Boström87713d02015-08-25 09:53:21 +0200315 case MSG_USE_CONSTRUCTOR_CERTIFICATE: {
316 rtc::ScopedRefMessageData<rtc::RTCCertificate>* param =
317 static_cast<rtc::ScopedRefMessageData<rtc::RTCCertificate>*>(
318 msg->pdata);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100319 RTC_LOG(LS_INFO) << "Using certificate supplied to the constructor.";
Henrik Boströmd8281982015-08-27 10:12:24 +0200320 SetCertificate(param->data());
Henrik Boström87713d02015-08-25 09:53:21 +0200321 delete param;
322 break;
323 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000324 default:
nissec80e7412017-01-11 05:56:46 -0800325 RTC_NOTREACHED();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000326 break;
327 }
328}
329
330void WebRtcSessionDescriptionFactory::InternalCreateOffer(
331 CreateSessionDescriptionRequest request) {
Steve Antond5585ca2017-10-23 14:49:26 -0700332 if (pc_->local_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700333 // If the needs-ice-restart flag is set as described by JSEP, we should
334 // generate an offer with a new ufrag/password to trigger an ICE restart.
335 for (cricket::MediaDescriptionOptions& options :
336 request.options.media_description_options) {
Steve Antond5585ca2017-10-23 14:49:26 -0700337 if (pc_->NeedsIceRestart(options.mid)) {
zhihuang1c378ed2017-08-17 14:10:50 -0700338 options.transport_options.ice_restart = true;
deadbeefd1a38b52016-12-10 13:15:33 -0800339 }
340 }
341 }
342
Steve Anton6fe1fba2018-12-11 10:15:23 -0800343 std::unique_ptr<cricket::SessionDescription> desc =
344 session_desc_factory_.CreateOffer(
345 request.options, pc_->local_description()
346 ? pc_->local_description()->description()
347 : nullptr);
348 if (!desc) {
349 PostCreateSessionDescriptionFailed(request.observer,
350 "Failed to initialize the offer.");
351 return;
352 }
353
wu@webrtc.org91053e72013-08-10 07:18:04 +0000354 // RFC 3264
355 // When issuing an offer that modifies the session,
356 // the "o=" line of the new SDP MUST be identical to that in the
357 // previous SDP, except that the version in the origin field MUST
358 // increment by one from the previous SDP.
359
360 // Just increase the version number by one each time when a new offer
361 // is created regardless if it's identical to the previous one or not.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200362 // The |session_version_| is a uint64_t, the wrap around should not happen.
nisseede5da42017-01-12 05:15:36 -0800363 RTC_DCHECK(session_version_ + 1 > session_version_);
Steve Anton6fe1fba2018-12-11 10:15:23 -0800364 auto offer = absl::make_unique<JsepSessionDescription>(
365 SdpType::kOffer, std::move(desc), session_id_,
366 rtc::ToString(session_version_++));
Steve Antond5585ca2017-10-23 14:49:26 -0700367 if (pc_->local_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700368 for (const cricket::MediaDescriptionOptions& options :
369 request.options.media_description_options) {
370 if (!options.transport_options.ice_restart) {
Steve Antond5585ca2017-10-23 14:49:26 -0700371 CopyCandidatesFromSessionDescription(pc_->local_description(),
Steve Antona3a92c22017-12-07 10:27:41 -0800372 options.mid, offer.get());
deadbeef0ed85b22016-02-23 17:24:52 -0800373 }
374 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000375 }
Steve Antona3a92c22017-12-07 10:27:41 -0800376 PostCreateSessionDescriptionSucceeded(request.observer, std::move(offer));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000377}
378
379void WebRtcSessionDescriptionFactory::InternalCreateAnswer(
380 CreateSessionDescriptionRequest request) {
Steve Antond5585ca2017-10-23 14:49:26 -0700381 if (pc_->remote_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700382 for (cricket::MediaDescriptionOptions& options :
383 request.options.media_description_options) {
deadbeef0ed85b22016-02-23 17:24:52 -0800384 // According to http://tools.ietf.org/html/rfc5245#section-9.2.1.1
385 // an answer should also contain new ICE ufrag and password if an offer
386 // has been received with new ufrag and password.
zhihuang1c378ed2017-08-17 14:10:50 -0700387 options.transport_options.ice_restart =
Steve Antond5585ca2017-10-23 14:49:26 -0700388 pc_->IceRestartPending(options.mid);
deadbeef0ed85b22016-02-23 17:24:52 -0800389 // We should pass the current SSL role to the transport description
390 // factory, if there is already an existing ongoing session.
391 rtc::SSLRole ssl_role;
Steve Antond5585ca2017-10-23 14:49:26 -0700392 if (pc_->GetSslRole(options.mid, &ssl_role)) {
zhihuang1c378ed2017-08-17 14:10:50 -0700393 options.transport_options.prefer_passive_role =
deadbeef0ed85b22016-02-23 17:24:52 -0800394 (rtc::SSL_SERVER == ssl_role);
395 }
396 }
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000397 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000398
Steve Anton6fe1fba2018-12-11 10:15:23 -0800399 std::unique_ptr<cricket::SessionDescription> desc =
400 session_desc_factory_.CreateAnswer(
401 pc_->remote_description() ? pc_->remote_description()->description()
402 : nullptr,
403 request.options,
404 pc_->local_description() ? pc_->local_description()->description()
405 : nullptr);
406 if (!desc) {
407 PostCreateSessionDescriptionFailed(request.observer,
408 "Failed to initialize the answer.");
409 return;
410 }
411
wu@webrtc.org91053e72013-08-10 07:18:04 +0000412 // RFC 3264
413 // If the answer is different from the offer in any way (different IP
414 // addresses, ports, etc.), the origin line MUST be different in the answer.
415 // In that case, the version number in the "o=" line of the answer is
416 // unrelated to the version number in the o line of the offer.
417 // Get a new version number by increasing the |session_version_answer_|.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200418 // The |session_version_| is a uint64_t, the wrap around should not happen.
nisseede5da42017-01-12 05:15:36 -0800419 RTC_DCHECK(session_version_ + 1 > session_version_);
Steve Anton6fe1fba2018-12-11 10:15:23 -0800420 auto answer = absl::make_unique<JsepSessionDescription>(
421 SdpType::kAnswer, std::move(desc), session_id_,
422 rtc::ToString(session_version_++));
Steve Antond5585ca2017-10-23 14:49:26 -0700423 if (pc_->local_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700424 // Include all local ICE candidates in the SessionDescription unless
425 // the remote peer has requested an ICE restart.
426 for (const cricket::MediaDescriptionOptions& options :
427 request.options.media_description_options) {
428 if (!options.transport_options.ice_restart) {
Steve Antond5585ca2017-10-23 14:49:26 -0700429 CopyCandidatesFromSessionDescription(pc_->local_description(),
Steve Antona3a92c22017-12-07 10:27:41 -0800430 options.mid, answer.get());
deadbeef0ed85b22016-02-23 17:24:52 -0800431 }
432 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000433 }
Steve Antona3a92c22017-12-07 10:27:41 -0800434 PostCreateSessionDescriptionSucceeded(request.observer, std::move(answer));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000435}
436
tommi0f620f42015-07-09 03:25:02 -0700437void WebRtcSessionDescriptionFactory::FailPendingRequests(
438 const std::string& reason) {
nisseede5da42017-01-12 05:15:36 -0800439 RTC_DCHECK(signaling_thread_->IsCurrent());
tommi0f620f42015-07-09 03:25:02 -0700440 while (!create_session_description_requests_.empty()) {
441 const CreateSessionDescriptionRequest& request =
442 create_session_description_requests_.front();
Yves Gerey665174f2018-06-19 15:03:05 +0200443 PostCreateSessionDescriptionFailed(
444 request.observer,
445 ((request.type == CreateSessionDescriptionRequest::kOffer)
446 ? "CreateOffer"
447 : "CreateAnswer") +
448 reason);
tommi0f620f42015-07-09 03:25:02 -0700449 create_session_description_requests_.pop();
450 }
451}
452
wu@webrtc.org91053e72013-08-10 07:18:04 +0000453void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionFailed(
Yves Gerey665174f2018-06-19 15:03:05 +0200454 CreateSessionDescriptionObserver* observer,
455 const std::string& error) {
Harald Alvestrand73771a82018-05-24 10:53:49 +0200456 CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(
457 observer, RTCError(RTCErrorType::INTERNAL_ERROR, std::string(error)));
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700458 signaling_thread_->Post(RTC_FROM_HERE, this,
459 MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100460 RTC_LOG(LS_ERROR) << "Create SDP failed: " << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000461}
462
463void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionSucceeded(
464 CreateSessionDescriptionObserver* observer,
Steve Antona3a92c22017-12-07 10:27:41 -0800465 std::unique_ptr<SessionDescriptionInterface> description) {
Harald Alvestrand73771a82018-05-24 10:53:49 +0200466 CreateSessionDescriptionMsg* msg =
467 new CreateSessionDescriptionMsg(observer, RTCError::OK());
Steve Antona3a92c22017-12-07 10:27:41 -0800468 msg->description = std::move(description);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700469 signaling_thread_->Post(RTC_FROM_HERE, this,
470 MSG_CREATE_SESSIONDESCRIPTION_SUCCESS, msg);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000471}
472
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200473void WebRtcSessionDescriptionFactory::OnCertificateRequestFailed() {
nisseede5da42017-01-12 05:15:36 -0800474 RTC_DCHECK(signaling_thread_->IsCurrent());
wu@webrtc.org91053e72013-08-10 07:18:04 +0000475
Mirko Bonadei675513b2017-11-09 11:09:25 +0100476 RTC_LOG(LS_ERROR) << "Asynchronous certificate generation request failed.";
Henrik Boström87713d02015-08-25 09:53:21 +0200477 certificate_request_state_ = CERTIFICATE_FAILED;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000478
tommi0f620f42015-07-09 03:25:02 -0700479 FailPendingRequests(kFailedDueToIdentityFailed);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000480}
481
Henrik Boströmd8281982015-08-27 10:12:24 +0200482void WebRtcSessionDescriptionFactory::SetCertificate(
483 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
henrikg91d6ede2015-09-17 00:24:34 -0700484 RTC_DCHECK(certificate);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100485 RTC_LOG(LS_VERBOSE) << "Setting new certificate.";
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000486
Henrik Boström87713d02015-08-25 09:53:21 +0200487 certificate_request_state_ = CERTIFICATE_SUCCEEDED;
Henrik Boströmd8281982015-08-27 10:12:24 +0200488 SignalCertificateReady(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000489
Henrik Boström3a14bf32015-08-31 09:27:58 +0200490 transport_desc_factory_.set_certificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000491 transport_desc_factory_.set_secure(cricket::SEC_ENABLED);
492
493 while (!create_session_description_requests_.empty()) {
494 if (create_session_description_requests_.front().type ==
495 CreateSessionDescriptionRequest::kOffer) {
496 InternalCreateOffer(create_session_description_requests_.front());
497 } else {
498 InternalCreateAnswer(create_session_description_requests_.front());
499 }
500 create_session_description_requests_.pop();
501 }
502}
wu@webrtc.org91053e72013-08-10 07:18:04 +0000503} // namespace webrtc