blob: 2a9dc3fbd8f05123ef33f49da3b440249c7ac0d5 [file] [log] [blame]
wu@webrtc.org91053e72013-08-10 07:18:041/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
wu@webrtc.org91053e72013-08-10 07:18:043 *
kjellanderb24317b2016-02-10 15:54:434 * 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:049 */
10
Steve Anton10542f22019-01-11 17:11:0011#include "pc/webrtc_session_description_factory.h"
wu@webrtc.org91053e72013-08-10 07:18:0412
Yves Gerey3e707812018-11-28 15:47:4913#include <stddef.h>
Harald Alvestrandd89ce532020-10-21 08:59:2214#include <algorithm>
Mirko Bonadei317a1f02019-09-17 15:06:1815#include <memory>
Steve Anton36b29d12017-10-30 16:57:4216#include <string>
Harald Alvestrandf01bd6c2020-10-23 13:30:4617#include <type_traits>
kwiberg0eb15ed2015-12-17 11:04:1518#include <utility>
Steve Anton36b29d12017-10-30 16:57:4219#include <vector>
kwiberg0eb15ed2015-12-17 11:04:1520
Steve Anton64b626b2019-01-29 01:25:2621#include "absl/algorithm/container.h"
Yves Gerey3e707812018-11-28 15:47:4922#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3123#include "api/jsep.h"
Steve Anton10542f22019-01-11 17:11:0024#include "api/jsep_session_description.h"
25#include "api/rtc_error.h"
Harald Alvestrandf01bd6c2020-10-23 13:30:4626#include "pc/sdp_state_provider.h"
Steve Anton10542f22019-01-11 17:11:0027#include "pc/session_description.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3128#include "rtc_base/checks.h"
Yves Gerey3e707812018-11-28 15:47:4929#include "rtc_base/location.h"
30#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 17:11:0031#include "rtc_base/ref_counted_object.h"
32#include "rtc_base/ssl_identity.h"
33#include "rtc_base/ssl_stream_adapter.h"
34#include "rtc_base/string_encode.h"
wu@webrtc.org91053e72013-08-10 07:18:0435
wu@webrtc.org364f2042013-11-20 21:49:4136using cricket::MediaSessionOptions;
Amit Hilbuchbcd39d42019-01-26 01:13:5637using rtc::UniqueRandomIdGenerator;
wu@webrtc.org364f2042013-11-20 21:49:4138
wu@webrtc.org91053e72013-08-10 07:18:0439namespace webrtc {
wu@webrtc.org91053e72013-08-10 07:18:0440namespace {
wu@webrtc.org91053e72013-08-10 07:18:0441static const char kFailedDueToIdentityFailed[] =
42 " failed because DTLS identity request failed";
tommi0f620f42015-07-09 10:25:0243static const char kFailedDueToSessionShutdown[] =
44 " failed because the session was shut down";
wu@webrtc.org91053e72013-08-10 07:18:0445
Peter Boström0c4e06b2015-10-07 10:23:2146static const uint64_t kInitSessionVersion = 2;
wu@webrtc.org91053e72013-08-10 07:18:0447
zhihuang1c378ed2017-08-17 21:10:5048// Check that each sender has a unique ID.
49static bool ValidMediaSessionOptions(
50 const cricket::MediaSessionOptions& session_options) {
51 std::vector<cricket::SenderOptions> sorted_senders;
52 for (const cricket::MediaDescriptionOptions& media_description_options :
53 session_options.media_description_options) {
54 sorted_senders.insert(sorted_senders.end(),
55 media_description_options.sender_options.begin(),
56 media_description_options.sender_options.end());
57 }
Steve Anton64b626b2019-01-29 01:25:2658 absl::c_sort(sorted_senders, [](const cricket::SenderOptions& sender1,
59 const cricket::SenderOptions& sender2) {
60 return sender1.track_id < sender2.track_id;
61 });
62 return absl::c_adjacent_find(sorted_senders,
63 [](const cricket::SenderOptions& sender1,
64 const cricket::SenderOptions& sender2) {
65 return sender1.track_id == sender2.track_id;
66 }) == sorted_senders.end();
wu@webrtc.org91053e72013-08-10 07:18:0467}
68
69enum {
70 MSG_CREATE_SESSIONDESCRIPTION_SUCCESS,
Henrik Boström87713d02015-08-25 07:53:2171 MSG_CREATE_SESSIONDESCRIPTION_FAILED,
72 MSG_USE_CONSTRUCTOR_CERTIFICATE
wu@webrtc.org91053e72013-08-10 07:18:0473};
74
buildbot@webrtc.orgd4e598d2014-07-29 17:36:5275struct CreateSessionDescriptionMsg : public rtc::MessageData {
wu@webrtc.org91053e72013-08-10 07:18:0476 explicit CreateSessionDescriptionMsg(
Harald Alvestrand73771a82018-05-24 08:53:4977 webrtc::CreateSessionDescriptionObserver* observer,
78 RTCError error_in)
79 : observer(observer), error(std::move(error_in)) {}
wu@webrtc.org91053e72013-08-10 07:18:0480
buildbot@webrtc.orgd4e598d2014-07-29 17:36:5281 rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer;
Harald Alvestrand73771a82018-05-24 08:53:4982 RTCError error;
kwibergd1fe2812016-04-27 13:47:2983 std::unique_ptr<webrtc::SessionDescriptionInterface> description;
wu@webrtc.org91053e72013-08-10 07:18:0484};
wu@webrtc.org91053e72013-08-10 07:18:0485} // namespace
86
Henrik Boströmd03c23b2016-06-01 09:44:1887void WebRtcCertificateGeneratorCallback::OnFailure() {
88 SignalRequestFailed();
jiayl@webrtc.org61e00b02015-03-04 22:17:3889}
90
Henrik Boströmd03c23b2016-06-01 09:44:1891void WebRtcCertificateGeneratorCallback::OnSuccess(
92 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
93 SignalCertificateReady(certificate);
jiayl@webrtc.org61e00b02015-03-04 22:17:3894}
95
wu@webrtc.org91053e72013-08-10 07:18:0496// static
97void WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
98 const SessionDescriptionInterface* source_desc,
deadbeef0ed85b22016-02-24 01:24:5299 const std::string& content_name,
wu@webrtc.org91053e72013-08-10 07:18:04100 SessionDescriptionInterface* dest_desc) {
deadbeef0ed85b22016-02-24 01:24:52101 if (!source_desc) {
wu@webrtc.org91053e72013-08-10 07:18:04102 return;
deadbeef0ed85b22016-02-24 01:24:52103 }
104 const cricket::ContentInfos& contents =
105 source_desc->description()->contents();
106 const cricket::ContentInfo* cinfo =
107 source_desc->description()->GetContentByName(content_name);
108 if (!cinfo) {
109 return;
110 }
111 size_t mediasection_index = static_cast<int>(cinfo - &contents[0]);
112 const IceCandidateCollection* source_candidates =
113 source_desc->candidates(mediasection_index);
114 const IceCandidateCollection* dest_candidates =
115 dest_desc->candidates(mediasection_index);
Taylor Brandstetter4eb1ddd2016-03-02 00:21:07116 if (!source_candidates || !dest_candidates) {
117 return;
118 }
deadbeef0ed85b22016-02-24 01:24:52119 for (size_t n = 0; n < source_candidates->count(); ++n) {
120 const IceCandidateInterface* new_candidate = source_candidates->at(n);
121 if (!dest_candidates->HasCandidate(new_candidate)) {
122 dest_desc->AddCandidate(source_candidates->at(n));
wu@webrtc.org91053e72013-08-10 07:18:04123 }
124 }
125}
126
127WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52128 rtc::Thread* signaling_thread,
wu@webrtc.org91053e72013-08-10 07:18:04129 cricket::ChannelManager* channel_manager,
Harald Alvestrandf01bd6c2020-10-23 13:30:46130 const SdpStateProvider* sdp_info,
wu@webrtc.org91053e72013-08-10 07:18:04131 const std::string& session_id,
Harald Alvestrandd89ce532020-10-21 08:59:22132 bool dtls_enabled,
Henrik Boströmd03c23b2016-06-01 09:44:18133 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
Amit Hilbuchbcd39d42019-01-26 01:13:56134 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate,
Harald Alvestranda0947872020-11-09 14:15:00135 UniqueRandomIdGenerator* ssrc_generator,
136 std::function<void(const rtc::scoped_refptr<rtc::RTCCertificate>&)>
137 on_certificate_ready)
Tomas Gunnarsson77baeee2020-09-24 20:39:21138 : signaling_thread_(signaling_thread),
Amit Hilbuchbcd39d42019-01-26 01:13:56139 session_desc_factory_(channel_manager,
140 &transport_desc_factory_,
141 ssrc_generator),
wu@webrtc.org91053e72013-08-10 07:18:04142 // RFC 4566 suggested a Network Time Protocol (NTP) format timestamp
143 // as the session id and session version. To simplify, it should be fine
144 // to just use a random number as session id and start version from
145 // |kInitSessionVersion|.
146 session_version_(kInitSessionVersion),
Harald Alvestrandd89ce532020-10-21 08:59:22147 cert_generator_(dtls_enabled ? std::move(cert_generator) : nullptr),
Harald Alvestrandf01bd6c2020-10-23 13:30:46148 sdp_info_(sdp_info),
wu@webrtc.org91053e72013-08-10 07:18:04149 session_id_(session_id),
Harald Alvestranda0947872020-11-09 14:15:00150 certificate_request_state_(CERTIFICATE_NOT_NEEDED),
151 on_certificate_ready_(on_certificate_ready) {
Henrik Boströmd03c23b2016-06-01 09:44:18152 RTC_DCHECK(signaling_thread_);
Harald Alvestrandd89ce532020-10-21 08:59:22153
Henrik Boströmd03c23b2016-06-01 09:44:18154 if (!dtls_enabled) {
Harald Alvestrandd89ce532020-10-21 08:59:22155 SetSdesPolicy(cricket::SEC_REQUIRED);
Mirko Bonadei675513b2017-11-09 10:09:25156 RTC_LOG(LS_VERBOSE) << "DTLS-SRTP disabled.";
Henrik Boströmd03c23b2016-06-01 09:44:18157 return;
158 }
159
Harald Alvestrandd89ce532020-10-21 08:59:22160 // SRTP-SDES is disabled if DTLS is on.
161 SetSdesPolicy(cricket::SEC_DISABLED);
Henrik Boströmd03c23b2016-06-01 09:44:18162 if (certificate) {
163 // Use |certificate|.
164 certificate_request_state_ = CERTIFICATE_WAITING;
165
Mirko Bonadei675513b2017-11-09 10:09:25166 RTC_LOG(LS_VERBOSE) << "DTLS-SRTP enabled; has certificate parameter.";
Henrik Boströmd03c23b2016-06-01 09:44:18167 // We already have a certificate but we wait to do |SetIdentity|; if we do
168 // it in the constructor then the caller has not had a chance to connect to
169 // |SignalCertificateReady|.
170 signaling_thread_->Post(
Taylor Brandstetter5d97a9a2016-06-10 21:17:27171 RTC_FROM_HERE, this, MSG_USE_CONSTRUCTOR_CERTIFICATE,
Henrik Boströmd03c23b2016-06-01 09:44:18172 new rtc::ScopedRefMessageData<rtc::RTCCertificate>(certificate));
173 } else {
174 // Generate certificate.
175 certificate_request_state_ = CERTIFICATE_WAITING;
176
177 rtc::scoped_refptr<WebRtcCertificateGeneratorCallback> callback(
178 new rtc::RefCountedObject<WebRtcCertificateGeneratorCallback>());
179 callback->SignalRequestFailed.connect(
180 this, &WebRtcSessionDescriptionFactory::OnCertificateRequestFailed);
181 callback->SignalCertificateReady.connect(
182 this, &WebRtcSessionDescriptionFactory::SetCertificate);
183
184 rtc::KeyParams key_params = rtc::KeyParams();
Mirko Bonadei675513b2017-11-09 10:09:25185 RTC_LOG(LS_VERBOSE)
Jonas Olsson45cc8902018-02-13 09:37:07186 << "DTLS-SRTP enabled; sending DTLS identity request (key type: "
187 << key_params.type() << ").";
Henrik Boströmd03c23b2016-06-01 09:44:18188
189 // Request certificate. This happens asynchronously, so that the caller gets
190 // a chance to connect to |SignalCertificateReady|.
Danil Chapovalov66cadcc2018-06-19 14:47:43191 cert_generator_->GenerateCertificateAsync(key_params, absl::nullopt,
Oskar Sundbom36f8f3e2017-11-16 09:54:27192 callback);
Henrik Boströmd03c23b2016-06-01 09:44:18193 }
Henrik Boström87713d02015-08-25 07:53:21194}
wu@webrtc.org91053e72013-08-10 07:18:04195
wu@webrtc.org91053e72013-08-10 07:18:04196WebRtcSessionDescriptionFactory::~WebRtcSessionDescriptionFactory() {
nisseede5da42017-01-12 13:15:36197 RTC_DCHECK(signaling_thread_->IsCurrent());
tommi0f620f42015-07-09 10:25:02198
199 // Fail any requests that were asked for before identity generation completed.
200 FailPendingRequests(kFailedDueToSessionShutdown);
201
202 // Process all pending notifications in the message queue. If we don't do
203 // this, requests will linger and not know they succeeded or failed.
204 rtc::MessageList list;
205 signaling_thread_->Clear(this, rtc::MQID_ANY, &list);
Henrik Boström87713d02015-08-25 07:53:21206 for (auto& msg : list) {
207 if (msg.message_id != MSG_USE_CONSTRUCTOR_CERTIFICATE) {
208 OnMessage(&msg);
209 } else {
210 // Skip MSG_USE_CONSTRUCTOR_CERTIFICATE because we don't want to trigger
211 // SetIdentity-related callbacks in the destructor. This can be a problem
212 // when WebRtcSession listens to the callback but it was the WebRtcSession
213 // destructor that caused WebRtcSessionDescriptionFactory's destruction.
214 // The callback is then ignored, leaking memory allocated by OnMessage for
215 // MSG_USE_CONSTRUCTOR_CERTIFICATE.
216 delete msg.pdata;
217 }
218 }
wu@webrtc.org91053e72013-08-10 07:18:04219}
220
221void WebRtcSessionDescriptionFactory::CreateOffer(
222 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 18:33:11223 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
224 const cricket::MediaSessionOptions& session_options) {
wu@webrtc.org91053e72013-08-10 07:18:04225 std::string error = "CreateOffer";
Henrik Boström87713d02015-08-25 07:53:21226 if (certificate_request_state_ == CERTIFICATE_FAILED) {
wu@webrtc.org91053e72013-08-10 07:18:04227 error += kFailedDueToIdentityFailed;
Mirko Bonadei675513b2017-11-09 10:09:25228 RTC_LOG(LS_ERROR) << error;
wu@webrtc.org91053e72013-08-10 07:18:04229 PostCreateSessionDescriptionFailed(observer, error);
230 return;
231 }
232
zhihuang1c378ed2017-08-17 21:10:50233 if (!ValidMediaSessionOptions(session_options)) {
234 error += " called with invalid session options";
Mirko Bonadei675513b2017-11-09 10:09:25235 RTC_LOG(LS_ERROR) << error;
wu@webrtc.org91053e72013-08-10 07:18:04236 PostCreateSessionDescriptionFailed(observer, error);
237 return;
238 }
239
wu@webrtc.org91053e72013-08-10 07:18:04240 CreateSessionDescriptionRequest request(
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16241 CreateSessionDescriptionRequest::kOffer, observer, session_options);
Henrik Boström87713d02015-08-25 07:53:21242 if (certificate_request_state_ == CERTIFICATE_WAITING) {
wu@webrtc.org91053e72013-08-10 07:18:04243 create_session_description_requests_.push(request);
244 } else {
nisseede5da42017-01-12 13:15:36245 RTC_DCHECK(certificate_request_state_ == CERTIFICATE_SUCCEEDED ||
246 certificate_request_state_ == CERTIFICATE_NOT_NEEDED);
wu@webrtc.org91053e72013-08-10 07:18:04247 InternalCreateOffer(request);
248 }
249}
250
251void WebRtcSessionDescriptionFactory::CreateAnswer(
252 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 18:33:11253 const cricket::MediaSessionOptions& session_options) {
wu@webrtc.org91053e72013-08-10 07:18:04254 std::string error = "CreateAnswer";
Henrik Boström87713d02015-08-25 07:53:21255 if (certificate_request_state_ == CERTIFICATE_FAILED) {
wu@webrtc.org91053e72013-08-10 07:18:04256 error += kFailedDueToIdentityFailed;
Mirko Bonadei675513b2017-11-09 10:09:25257 RTC_LOG(LS_ERROR) << error;
wu@webrtc.org91053e72013-08-10 07:18:04258 PostCreateSessionDescriptionFailed(observer, error);
259 return;
260 }
Harald Alvestrandf01bd6c2020-10-23 13:30:46261 if (!sdp_info_->remote_description()) {
wu@webrtc.org91053e72013-08-10 07:18:04262 error += " can't be called before SetRemoteDescription.";
Mirko Bonadei675513b2017-11-09 10:09:25263 RTC_LOG(LS_ERROR) << error;
wu@webrtc.org91053e72013-08-10 07:18:04264 PostCreateSessionDescriptionFailed(observer, error);
265 return;
266 }
Harald Alvestrandf01bd6c2020-10-23 13:30:46267 if (sdp_info_->remote_description()->GetType() != SdpType::kOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04268 error += " failed because remote_description is not an offer.";
Mirko Bonadei675513b2017-11-09 10:09:25269 RTC_LOG(LS_ERROR) << error;
wu@webrtc.org91053e72013-08-10 07:18:04270 PostCreateSessionDescriptionFailed(observer, error);
271 return;
272 }
273
zhihuang1c378ed2017-08-17 21:10:50274 if (!ValidMediaSessionOptions(session_options)) {
275 error += " called with invalid session options.";
Mirko Bonadei675513b2017-11-09 10:09:25276 RTC_LOG(LS_ERROR) << error;
wu@webrtc.org91053e72013-08-10 07:18:04277 PostCreateSessionDescriptionFailed(observer, error);
278 return;
279 }
wu@webrtc.org91053e72013-08-10 07:18:04280
281 CreateSessionDescriptionRequest request(
deadbeefab9b2d12015-10-14 18:33:11282 CreateSessionDescriptionRequest::kAnswer, observer, session_options);
Henrik Boström87713d02015-08-25 07:53:21283 if (certificate_request_state_ == CERTIFICATE_WAITING) {
wu@webrtc.org91053e72013-08-10 07:18:04284 create_session_description_requests_.push(request);
285 } else {
nisseede5da42017-01-12 13:15:36286 RTC_DCHECK(certificate_request_state_ == CERTIFICATE_SUCCEEDED ||
287 certificate_request_state_ == CERTIFICATE_NOT_NEEDED);
wu@webrtc.org91053e72013-08-10 07:18:04288 InternalCreateAnswer(request);
289 }
290}
291
henrike@webrtc.orgb90991d2014-03-04 19:54:57292void WebRtcSessionDescriptionFactory::SetSdesPolicy(
293 cricket::SecurePolicy secure_policy) {
wu@webrtc.org91053e72013-08-10 07:18:04294 session_desc_factory_.set_secure(secure_policy);
295}
296
henrike@webrtc.orgb90991d2014-03-04 19:54:57297cricket::SecurePolicy WebRtcSessionDescriptionFactory::SdesPolicy() const {
wu@webrtc.org91053e72013-08-10 07:18:04298 return session_desc_factory_.secure();
299}
300
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52301void WebRtcSessionDescriptionFactory::OnMessage(rtc::Message* msg) {
wu@webrtc.org91053e72013-08-10 07:18:04302 switch (msg->message_id) {
303 case MSG_CREATE_SESSIONDESCRIPTION_SUCCESS: {
304 CreateSessionDescriptionMsg* param =
305 static_cast<CreateSessionDescriptionMsg*>(msg->pdata);
306 param->observer->OnSuccess(param->description.release());
307 delete param;
308 break;
309 }
310 case MSG_CREATE_SESSIONDESCRIPTION_FAILED: {
311 CreateSessionDescriptionMsg* param =
312 static_cast<CreateSessionDescriptionMsg*>(msg->pdata);
Harald Alvestrand73771a82018-05-24 08:53:49313 param->observer->OnFailure(std::move(param->error));
wu@webrtc.org91053e72013-08-10 07:18:04314 delete param;
315 break;
316 }
Henrik Boström87713d02015-08-25 07:53:21317 case MSG_USE_CONSTRUCTOR_CERTIFICATE: {
318 rtc::ScopedRefMessageData<rtc::RTCCertificate>* param =
319 static_cast<rtc::ScopedRefMessageData<rtc::RTCCertificate>*>(
320 msg->pdata);
Mirko Bonadei675513b2017-11-09 10:09:25321 RTC_LOG(LS_INFO) << "Using certificate supplied to the constructor.";
Henrik Boströmd8281982015-08-27 08:12:24322 SetCertificate(param->data());
Henrik Boström87713d02015-08-25 07:53:21323 delete param;
324 break;
325 }
wu@webrtc.org91053e72013-08-10 07:18:04326 default:
nissec80e7412017-01-11 13:56:46327 RTC_NOTREACHED();
wu@webrtc.org91053e72013-08-10 07:18:04328 break;
329 }
330}
331
332void WebRtcSessionDescriptionFactory::InternalCreateOffer(
333 CreateSessionDescriptionRequest request) {
Harald Alvestrandf01bd6c2020-10-23 13:30:46334 if (sdp_info_->local_description()) {
zhihuang1c378ed2017-08-17 21:10:50335 // If the needs-ice-restart flag is set as described by JSEP, we should
336 // generate an offer with a new ufrag/password to trigger an ICE restart.
337 for (cricket::MediaDescriptionOptions& options :
338 request.options.media_description_options) {
Harald Alvestrandf01bd6c2020-10-23 13:30:46339 if (sdp_info_->NeedsIceRestart(options.mid)) {
zhihuang1c378ed2017-08-17 21:10:50340 options.transport_options.ice_restart = true;
deadbeefd1a38b52016-12-10 21:15:33341 }
342 }
343 }
344
Steve Anton6fe1fba2018-12-11 18:15:23345 std::unique_ptr<cricket::SessionDescription> desc =
346 session_desc_factory_.CreateOffer(
Harald Alvestrandf01bd6c2020-10-23 13:30:46347 request.options, sdp_info_->local_description()
348 ? sdp_info_->local_description()->description()
349 : nullptr);
Steve Anton6fe1fba2018-12-11 18:15:23350 if (!desc) {
351 PostCreateSessionDescriptionFailed(request.observer,
352 "Failed to initialize the offer.");
353 return;
354 }
355
wu@webrtc.org91053e72013-08-10 07:18:04356 // RFC 3264
357 // When issuing an offer that modifies the session,
358 // the "o=" line of the new SDP MUST be identical to that in the
359 // previous SDP, except that the version in the origin field MUST
360 // increment by one from the previous SDP.
361
362 // Just increase the version number by one each time when a new offer
363 // is created regardless if it's identical to the previous one or not.
Peter Boström0c4e06b2015-10-07 10:23:21364 // The |session_version_| is a uint64_t, the wrap around should not happen.
nisseede5da42017-01-12 13:15:36365 RTC_DCHECK(session_version_ + 1 > session_version_);
Mirko Bonadei317a1f02019-09-17 15:06:18366 auto offer = std::make_unique<JsepSessionDescription>(
Steve Anton6fe1fba2018-12-11 18:15:23367 SdpType::kOffer, std::move(desc), session_id_,
368 rtc::ToString(session_version_++));
Harald Alvestrandf01bd6c2020-10-23 13:30:46369 if (sdp_info_->local_description()) {
zhihuang1c378ed2017-08-17 21:10:50370 for (const cricket::MediaDescriptionOptions& options :
371 request.options.media_description_options) {
372 if (!options.transport_options.ice_restart) {
Harald Alvestrandf01bd6c2020-10-23 13:30:46373 CopyCandidatesFromSessionDescription(sdp_info_->local_description(),
Steve Antona3a92c22017-12-07 18:27:41374 options.mid, offer.get());
deadbeef0ed85b22016-02-24 01:24:52375 }
376 }
wu@webrtc.org91053e72013-08-10 07:18:04377 }
Steve Antona3a92c22017-12-07 18:27:41378 PostCreateSessionDescriptionSucceeded(request.observer, std::move(offer));
wu@webrtc.org91053e72013-08-10 07:18:04379}
380
381void WebRtcSessionDescriptionFactory::InternalCreateAnswer(
382 CreateSessionDescriptionRequest request) {
Harald Alvestrandf01bd6c2020-10-23 13:30:46383 if (sdp_info_->remote_description()) {
zhihuang1c378ed2017-08-17 21:10:50384 for (cricket::MediaDescriptionOptions& options :
385 request.options.media_description_options) {
deadbeef0ed85b22016-02-24 01:24:52386 // According to http://tools.ietf.org/html/rfc5245#section-9.2.1.1
387 // an answer should also contain new ICE ufrag and password if an offer
388 // has been received with new ufrag and password.
zhihuang1c378ed2017-08-17 21:10:50389 options.transport_options.ice_restart =
Harald Alvestrandf01bd6c2020-10-23 13:30:46390 sdp_info_->IceRestartPending(options.mid);
Harald Alvestrandd89ce532020-10-21 08:59:22391 // We should pass the current DTLS role to the transport description
deadbeef0ed85b22016-02-24 01:24:52392 // factory, if there is already an existing ongoing session.
Harald Alvestrandd89ce532020-10-21 08:59:22393 absl::optional<rtc::SSLRole> dtls_role =
Harald Alvestrandf01bd6c2020-10-23 13:30:46394 sdp_info_->GetDtlsRole(options.mid);
Harald Alvestrandd89ce532020-10-21 08:59:22395 if (dtls_role) {
zhihuang1c378ed2017-08-17 21:10:50396 options.transport_options.prefer_passive_role =
Harald Alvestrandd89ce532020-10-21 08:59:22397 (rtc::SSL_SERVER == *dtls_role);
deadbeef0ed85b22016-02-24 01:24:52398 }
399 }
sergeyu@chromium.org0be6aa02013-08-23 23:21:25400 }
wu@webrtc.org91053e72013-08-10 07:18:04401
Steve Anton6fe1fba2018-12-11 18:15:23402 std::unique_ptr<cricket::SessionDescription> desc =
403 session_desc_factory_.CreateAnswer(
Harald Alvestrandf01bd6c2020-10-23 13:30:46404 sdp_info_->remote_description()
405 ? sdp_info_->remote_description()->description()
Harald Alvestrandd89ce532020-10-21 08:59:22406 : nullptr,
Steve Anton6fe1fba2018-12-11 18:15:23407 request.options,
Harald Alvestrandf01bd6c2020-10-23 13:30:46408 sdp_info_->local_description()
409 ? sdp_info_->local_description()->description()
Harald Alvestrandd89ce532020-10-21 08:59:22410 : nullptr);
Steve Anton6fe1fba2018-12-11 18:15:23411 if (!desc) {
412 PostCreateSessionDescriptionFailed(request.observer,
413 "Failed to initialize the answer.");
414 return;
415 }
416
wu@webrtc.org91053e72013-08-10 07:18:04417 // RFC 3264
418 // If the answer is different from the offer in any way (different IP
419 // addresses, ports, etc.), the origin line MUST be different in the answer.
420 // In that case, the version number in the "o=" line of the answer is
421 // unrelated to the version number in the o line of the offer.
422 // Get a new version number by increasing the |session_version_answer_|.
Peter Boström0c4e06b2015-10-07 10:23:21423 // The |session_version_| is a uint64_t, the wrap around should not happen.
nisseede5da42017-01-12 13:15:36424 RTC_DCHECK(session_version_ + 1 > session_version_);
Mirko Bonadei317a1f02019-09-17 15:06:18425 auto answer = std::make_unique<JsepSessionDescription>(
Steve Anton6fe1fba2018-12-11 18:15:23426 SdpType::kAnswer, std::move(desc), session_id_,
427 rtc::ToString(session_version_++));
Harald Alvestrandf01bd6c2020-10-23 13:30:46428 if (sdp_info_->local_description()) {
zhihuang1c378ed2017-08-17 21:10:50429 // Include all local ICE candidates in the SessionDescription unless
430 // the remote peer has requested an ICE restart.
431 for (const cricket::MediaDescriptionOptions& options :
432 request.options.media_description_options) {
433 if (!options.transport_options.ice_restart) {
Harald Alvestrandf01bd6c2020-10-23 13:30:46434 CopyCandidatesFromSessionDescription(sdp_info_->local_description(),
Steve Antona3a92c22017-12-07 18:27:41435 options.mid, answer.get());
deadbeef0ed85b22016-02-24 01:24:52436 }
437 }
wu@webrtc.org91053e72013-08-10 07:18:04438 }
Steve Antona3a92c22017-12-07 18:27:41439 PostCreateSessionDescriptionSucceeded(request.observer, std::move(answer));
wu@webrtc.org91053e72013-08-10 07:18:04440}
441
tommi0f620f42015-07-09 10:25:02442void WebRtcSessionDescriptionFactory::FailPendingRequests(
443 const std::string& reason) {
nisseede5da42017-01-12 13:15:36444 RTC_DCHECK(signaling_thread_->IsCurrent());
tommi0f620f42015-07-09 10:25:02445 while (!create_session_description_requests_.empty()) {
446 const CreateSessionDescriptionRequest& request =
447 create_session_description_requests_.front();
Yves Gerey665174f2018-06-19 13:03:05448 PostCreateSessionDescriptionFailed(
449 request.observer,
450 ((request.type == CreateSessionDescriptionRequest::kOffer)
451 ? "CreateOffer"
452 : "CreateAnswer") +
453 reason);
tommi0f620f42015-07-09 10:25:02454 create_session_description_requests_.pop();
455 }
456}
457
wu@webrtc.org91053e72013-08-10 07:18:04458void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionFailed(
Yves Gerey665174f2018-06-19 13:03:05459 CreateSessionDescriptionObserver* observer,
460 const std::string& error) {
Harald Alvestrand73771a82018-05-24 08:53:49461 CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(
462 observer, RTCError(RTCErrorType::INTERNAL_ERROR, std::string(error)));
Taylor Brandstetter5d97a9a2016-06-10 21:17:27463 signaling_thread_->Post(RTC_FROM_HERE, this,
464 MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg);
Mirko Bonadei675513b2017-11-09 10:09:25465 RTC_LOG(LS_ERROR) << "Create SDP failed: " << error;
wu@webrtc.org91053e72013-08-10 07:18:04466}
467
468void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionSucceeded(
469 CreateSessionDescriptionObserver* observer,
Steve Antona3a92c22017-12-07 18:27:41470 std::unique_ptr<SessionDescriptionInterface> description) {
Harald Alvestrand73771a82018-05-24 08:53:49471 CreateSessionDescriptionMsg* msg =
472 new CreateSessionDescriptionMsg(observer, RTCError::OK());
Steve Antona3a92c22017-12-07 18:27:41473 msg->description = std::move(description);
Taylor Brandstetter5d97a9a2016-06-10 21:17:27474 signaling_thread_->Post(RTC_FROM_HERE, this,
475 MSG_CREATE_SESSIONDESCRIPTION_SUCCESS, msg);
wu@webrtc.org91053e72013-08-10 07:18:04476}
477
Henrik Boströmd03c23b2016-06-01 09:44:18478void WebRtcSessionDescriptionFactory::OnCertificateRequestFailed() {
nisseede5da42017-01-12 13:15:36479 RTC_DCHECK(signaling_thread_->IsCurrent());
wu@webrtc.org91053e72013-08-10 07:18:04480
Mirko Bonadei675513b2017-11-09 10:09:25481 RTC_LOG(LS_ERROR) << "Asynchronous certificate generation request failed.";
Henrik Boström87713d02015-08-25 07:53:21482 certificate_request_state_ = CERTIFICATE_FAILED;
wu@webrtc.org91053e72013-08-10 07:18:04483
tommi0f620f42015-07-09 10:25:02484 FailPendingRequests(kFailedDueToIdentityFailed);
wu@webrtc.org91053e72013-08-10 07:18:04485}
486
Henrik Boströmd8281982015-08-27 08:12:24487void WebRtcSessionDescriptionFactory::SetCertificate(
488 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
henrikg91d6ede2015-09-17 07:24:34489 RTC_DCHECK(certificate);
Mirko Bonadei675513b2017-11-09 10:09:25490 RTC_LOG(LS_VERBOSE) << "Setting new certificate.";
jiayl@webrtc.org61e00b02015-03-04 22:17:38491
Henrik Boström87713d02015-08-25 07:53:21492 certificate_request_state_ = CERTIFICATE_SUCCEEDED;
Harald Alvestranda0947872020-11-09 14:15:00493
494 on_certificate_ready_(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04495
Henrik Boström3a14bf32015-08-31 07:27:58496 transport_desc_factory_.set_certificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04497 transport_desc_factory_.set_secure(cricket::SEC_ENABLED);
498
499 while (!create_session_description_requests_.empty()) {
500 if (create_session_description_requests_.front().type ==
501 CreateSessionDescriptionRequest::kOffer) {
502 InternalCreateOffer(create_session_description_requests_.front());
503 } else {
504 InternalCreateAnswer(create_session_description_requests_.front());
505 }
506 create_session_description_requests_.pop();
507 }
508}
wu@webrtc.org91053e72013-08-10 07:18:04509} // namespace webrtc