blob: aaef7fdeb67b0515ad81e9314e7c0b4eaf94d8c2 [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Mirko Bonadei317a1f02019-09-17 17:06:18 +020015#include <memory>
Steve Anton36b29d12017-10-30 09:57:42 -070016#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080017#include <utility>
Steve Anton36b29d12017-10-30 09:57:42 -070018#include <vector>
kwiberg0eb15ed2015-12-17 03:04:15 -080019
Steve Anton64b626b2019-01-28 17:25:26 -080020#include "absl/algorithm/container.h"
Yves Gerey3e707812018-11-28 16:47:49 +010021#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "api/jsep.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "api/jsep_session_description.h"
24#include "api/rtc_error.h"
25#include "pc/session_description.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/checks.h"
Yves Gerey3e707812018-11-28 16:47:49 +010027#include "rtc_base/location.h"
28#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#include "rtc_base/ref_counted_object.h"
30#include "rtc_base/ssl_identity.h"
31#include "rtc_base/ssl_stream_adapter.h"
32#include "rtc_base/string_encode.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000033
wu@webrtc.org364f2042013-11-20 21:49:41 +000034using cricket::MediaSessionOptions;
Amit Hilbuchbcd39d42019-01-25 17:13:56 -080035using rtc::UniqueRandomIdGenerator;
wu@webrtc.org364f2042013-11-20 21:49:41 +000036
wu@webrtc.org91053e72013-08-10 07:18:04 +000037namespace webrtc {
wu@webrtc.org91053e72013-08-10 07:18:04 +000038namespace {
wu@webrtc.org91053e72013-08-10 07:18:04 +000039static const char kFailedDueToIdentityFailed[] =
40 " failed because DTLS identity request failed";
tommi0f620f42015-07-09 03:25:02 -070041static const char kFailedDueToSessionShutdown[] =
42 " failed because the session was shut down";
wu@webrtc.org91053e72013-08-10 07:18:04 +000043
Peter Boström0c4e06b2015-10-07 12:23:21 +020044static const uint64_t kInitSessionVersion = 2;
wu@webrtc.org91053e72013-08-10 07:18:04 +000045
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 }
Steve Anton64b626b2019-01-28 17:25:26 -080056 absl::c_sort(sorted_senders, [](const cricket::SenderOptions& sender1,
57 const cricket::SenderOptions& sender2) {
58 return sender1.track_id < sender2.track_id;
59 });
60 return absl::c_adjacent_find(sorted_senders,
61 [](const cricket::SenderOptions& sender1,
62 const cricket::SenderOptions& sender2) {
63 return sender1.track_id == sender2.track_id;
64 }) == sorted_senders.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +000065}
66
67enum {
68 MSG_CREATE_SESSIONDESCRIPTION_SUCCESS,
Henrik Boström87713d02015-08-25 09:53:21 +020069 MSG_CREATE_SESSIONDESCRIPTION_FAILED,
70 MSG_USE_CONSTRUCTOR_CERTIFICATE
wu@webrtc.org91053e72013-08-10 07:18:04 +000071};
72
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000073struct CreateSessionDescriptionMsg : public rtc::MessageData {
wu@webrtc.org91053e72013-08-10 07:18:04 +000074 explicit CreateSessionDescriptionMsg(
Harald Alvestrand73771a82018-05-24 10:53:49 +020075 webrtc::CreateSessionDescriptionObserver* observer,
76 RTCError error_in)
77 : observer(observer), error(std::move(error_in)) {}
wu@webrtc.org91053e72013-08-10 07:18:04 +000078
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000079 rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer;
Harald Alvestrand73771a82018-05-24 10:53:49 +020080 RTCError error;
kwibergd1fe2812016-04-27 06:47:29 -070081 std::unique_ptr<webrtc::SessionDescriptionInterface> description;
wu@webrtc.org91053e72013-08-10 07:18:04 +000082};
wu@webrtc.org91053e72013-08-10 07:18:04 +000083} // namespace
84
Henrik Boströmd03c23b2016-06-01 11:44:18 +020085void WebRtcCertificateGeneratorCallback::OnFailure() {
86 SignalRequestFailed();
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000087}
88
Henrik Boströmd03c23b2016-06-01 11:44:18 +020089void WebRtcCertificateGeneratorCallback::OnSuccess(
90 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
91 SignalCertificateReady(certificate);
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000092}
93
wu@webrtc.org91053e72013-08-10 07:18:04 +000094// static
95void WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
96 const SessionDescriptionInterface* source_desc,
deadbeef0ed85b22016-02-23 17:24:52 -080097 const std::string& content_name,
wu@webrtc.org91053e72013-08-10 07:18:04 +000098 SessionDescriptionInterface* dest_desc) {
deadbeef0ed85b22016-02-23 17:24:52 -080099 if (!source_desc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000100 return;
deadbeef0ed85b22016-02-23 17:24:52 -0800101 }
102 const cricket::ContentInfos& contents =
103 source_desc->description()->contents();
104 const cricket::ContentInfo* cinfo =
105 source_desc->description()->GetContentByName(content_name);
106 if (!cinfo) {
107 return;
108 }
109 size_t mediasection_index = static_cast<int>(cinfo - &contents[0]);
110 const IceCandidateCollection* source_candidates =
111 source_desc->candidates(mediasection_index);
112 const IceCandidateCollection* dest_candidates =
113 dest_desc->candidates(mediasection_index);
Taylor Brandstetter4eb1ddd2016-03-01 16:21:07 -0800114 if (!source_candidates || !dest_candidates) {
115 return;
116 }
deadbeef0ed85b22016-02-23 17:24:52 -0800117 for (size_t n = 0; n < source_candidates->count(); ++n) {
118 const IceCandidateInterface* new_candidate = source_candidates->at(n);
119 if (!dest_candidates->HasCandidate(new_candidate)) {
120 dest_desc->AddCandidate(source_candidates->at(n));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000121 }
122 }
123}
124
125WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000126 rtc::Thread* signaling_thread,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000127 cricket::ChannelManager* channel_manager,
Steve Anton2d8609c2018-01-23 16:38:46 -0800128 PeerConnectionInternal* pc,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000129 const std::string& session_id,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200130 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
Amit Hilbuchbcd39d42019-01-25 17:13:56 -0800131 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate,
132 UniqueRandomIdGenerator* ssrc_generator)
wu@webrtc.org91053e72013-08-10 07:18:04 +0000133 : signaling_thread_(signaling_thread),
Amit Hilbuchbcd39d42019-01-25 17:13:56 -0800134 session_desc_factory_(channel_manager,
135 &transport_desc_factory_,
136 ssrc_generator),
wu@webrtc.org91053e72013-08-10 07:18:04 +0000137 // RFC 4566 suggested a Network Time Protocol (NTP) format timestamp
138 // as the session id and session version. To simplify, it should be fine
139 // to just use a random number as session id and start version from
140 // |kInitSessionVersion|.
141 session_version_(kInitSessionVersion),
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200142 cert_generator_(std::move(cert_generator)),
Steve Antond5585ca2017-10-23 14:49:26 -0700143 pc_(pc),
wu@webrtc.org91053e72013-08-10 07:18:04 +0000144 session_id_(session_id),
Henrik Boström87713d02015-08-25 09:53:21 +0200145 certificate_request_state_(CERTIFICATE_NOT_NEEDED) {
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200146 RTC_DCHECK(signaling_thread_);
Steve Antond5585ca2017-10-23 14:49:26 -0700147 RTC_DCHECK(!(cert_generator_ && certificate));
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200148 bool dtls_enabled = cert_generator_ || certificate;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000149 // SRTP-SDES is disabled if DTLS is on.
150 SetSdesPolicy(dtls_enabled ? cricket::SEC_DISABLED : cricket::SEC_REQUIRED);
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200151 if (!dtls_enabled) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100152 RTC_LOG(LS_VERBOSE) << "DTLS-SRTP disabled.";
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200153 return;
154 }
155
156 if (certificate) {
157 // Use |certificate|.
158 certificate_request_state_ = CERTIFICATE_WAITING;
159
Mirko Bonadei675513b2017-11-09 11:09:25 +0100160 RTC_LOG(LS_VERBOSE) << "DTLS-SRTP enabled; has certificate parameter.";
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200161 // We already have a certificate but we wait to do |SetIdentity|; if we do
162 // it in the constructor then the caller has not had a chance to connect to
163 // |SignalCertificateReady|.
164 signaling_thread_->Post(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700165 RTC_FROM_HERE, this, MSG_USE_CONSTRUCTOR_CERTIFICATE,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200166 new rtc::ScopedRefMessageData<rtc::RTCCertificate>(certificate));
167 } else {
168 // Generate certificate.
169 certificate_request_state_ = CERTIFICATE_WAITING;
170
171 rtc::scoped_refptr<WebRtcCertificateGeneratorCallback> callback(
172 new rtc::RefCountedObject<WebRtcCertificateGeneratorCallback>());
173 callback->SignalRequestFailed.connect(
174 this, &WebRtcSessionDescriptionFactory::OnCertificateRequestFailed);
175 callback->SignalCertificateReady.connect(
176 this, &WebRtcSessionDescriptionFactory::SetCertificate);
177
178 rtc::KeyParams key_params = rtc::KeyParams();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100179 RTC_LOG(LS_VERBOSE)
Jonas Olsson45cc8902018-02-13 10:37:07 +0100180 << "DTLS-SRTP enabled; sending DTLS identity request (key type: "
181 << key_params.type() << ").";
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200182
183 // Request certificate. This happens asynchronously, so that the caller gets
184 // a chance to connect to |SignalCertificateReady|.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200185 cert_generator_->GenerateCertificateAsync(key_params, absl::nullopt,
Oskar Sundbom36f8f3e2017-11-16 10:54:27 +0100186 callback);
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200187 }
Henrik Boström87713d02015-08-25 09:53:21 +0200188}
wu@webrtc.org91053e72013-08-10 07:18:04 +0000189
wu@webrtc.org91053e72013-08-10 07:18:04 +0000190WebRtcSessionDescriptionFactory::~WebRtcSessionDescriptionFactory() {
nisseede5da42017-01-12 05:15:36 -0800191 RTC_DCHECK(signaling_thread_->IsCurrent());
tommi0f620f42015-07-09 03:25:02 -0700192
193 // Fail any requests that were asked for before identity generation completed.
194 FailPendingRequests(kFailedDueToSessionShutdown);
195
196 // Process all pending notifications in the message queue. If we don't do
197 // this, requests will linger and not know they succeeded or failed.
198 rtc::MessageList list;
199 signaling_thread_->Clear(this, rtc::MQID_ANY, &list);
Henrik Boström87713d02015-08-25 09:53:21 +0200200 for (auto& msg : list) {
201 if (msg.message_id != MSG_USE_CONSTRUCTOR_CERTIFICATE) {
202 OnMessage(&msg);
203 } else {
204 // Skip MSG_USE_CONSTRUCTOR_CERTIFICATE because we don't want to trigger
205 // SetIdentity-related callbacks in the destructor. This can be a problem
206 // when WebRtcSession listens to the callback but it was the WebRtcSession
207 // destructor that caused WebRtcSessionDescriptionFactory's destruction.
208 // The callback is then ignored, leaking memory allocated by OnMessage for
209 // MSG_USE_CONSTRUCTOR_CERTIFICATE.
210 delete msg.pdata;
211 }
212 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000213}
214
215void WebRtcSessionDescriptionFactory::CreateOffer(
216 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700217 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
218 const cricket::MediaSessionOptions& session_options) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000219 std::string error = "CreateOffer";
Henrik Boström87713d02015-08-25 09:53:21 +0200220 if (certificate_request_state_ == CERTIFICATE_FAILED) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000221 error += kFailedDueToIdentityFailed;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100222 RTC_LOG(LS_ERROR) << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000223 PostCreateSessionDescriptionFailed(observer, error);
224 return;
225 }
226
zhihuang1c378ed2017-08-17 14:10:50 -0700227 if (!ValidMediaSessionOptions(session_options)) {
228 error += " called with invalid session options";
Mirko Bonadei675513b2017-11-09 11:09:25 +0100229 RTC_LOG(LS_ERROR) << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000230 PostCreateSessionDescriptionFailed(observer, error);
231 return;
232 }
233
wu@webrtc.org91053e72013-08-10 07:18:04 +0000234 CreateSessionDescriptionRequest request(
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000235 CreateSessionDescriptionRequest::kOffer, observer, session_options);
Henrik Boström87713d02015-08-25 09:53:21 +0200236 if (certificate_request_state_ == CERTIFICATE_WAITING) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000237 create_session_description_requests_.push(request);
238 } else {
nisseede5da42017-01-12 05:15:36 -0800239 RTC_DCHECK(certificate_request_state_ == CERTIFICATE_SUCCEEDED ||
240 certificate_request_state_ == CERTIFICATE_NOT_NEEDED);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000241 InternalCreateOffer(request);
242 }
243}
244
245void WebRtcSessionDescriptionFactory::CreateAnswer(
246 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700247 const cricket::MediaSessionOptions& session_options) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000248 std::string error = "CreateAnswer";
Henrik Boström87713d02015-08-25 09:53:21 +0200249 if (certificate_request_state_ == CERTIFICATE_FAILED) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000250 error += kFailedDueToIdentityFailed;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100251 RTC_LOG(LS_ERROR) << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000252 PostCreateSessionDescriptionFailed(observer, error);
253 return;
254 }
Steve Antond5585ca2017-10-23 14:49:26 -0700255 if (!pc_->remote_description()) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000256 error += " can't be called before SetRemoteDescription.";
Mirko Bonadei675513b2017-11-09 11:09:25 +0100257 RTC_LOG(LS_ERROR) << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000258 PostCreateSessionDescriptionFailed(observer, error);
259 return;
260 }
Steve Antona3a92c22017-12-07 10:27:41 -0800261 if (pc_->remote_description()->GetType() != SdpType::kOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000262 error += " failed because remote_description is not an offer.";
Mirko Bonadei675513b2017-11-09 11:09:25 +0100263 RTC_LOG(LS_ERROR) << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000264 PostCreateSessionDescriptionFailed(observer, error);
265 return;
266 }
267
zhihuang1c378ed2017-08-17 14:10:50 -0700268 if (!ValidMediaSessionOptions(session_options)) {
269 error += " called with invalid session options.";
Mirko Bonadei675513b2017-11-09 11:09:25 +0100270 RTC_LOG(LS_ERROR) << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000271 PostCreateSessionDescriptionFailed(observer, error);
272 return;
273 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000274
275 CreateSessionDescriptionRequest request(
deadbeefab9b2d12015-10-14 11:33:11 -0700276 CreateSessionDescriptionRequest::kAnswer, observer, session_options);
Henrik Boström87713d02015-08-25 09:53:21 +0200277 if (certificate_request_state_ == CERTIFICATE_WAITING) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000278 create_session_description_requests_.push(request);
279 } else {
nisseede5da42017-01-12 05:15:36 -0800280 RTC_DCHECK(certificate_request_state_ == CERTIFICATE_SUCCEEDED ||
281 certificate_request_state_ == CERTIFICATE_NOT_NEEDED);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000282 InternalCreateAnswer(request);
283 }
284}
285
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000286void WebRtcSessionDescriptionFactory::SetSdesPolicy(
287 cricket::SecurePolicy secure_policy) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000288 session_desc_factory_.set_secure(secure_policy);
289}
290
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000291cricket::SecurePolicy WebRtcSessionDescriptionFactory::SdesPolicy() const {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000292 return session_desc_factory_.secure();
293}
294
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000295void WebRtcSessionDescriptionFactory::OnMessage(rtc::Message* msg) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000296 switch (msg->message_id) {
297 case MSG_CREATE_SESSIONDESCRIPTION_SUCCESS: {
298 CreateSessionDescriptionMsg* param =
299 static_cast<CreateSessionDescriptionMsg*>(msg->pdata);
300 param->observer->OnSuccess(param->description.release());
301 delete param;
302 break;
303 }
304 case MSG_CREATE_SESSIONDESCRIPTION_FAILED: {
305 CreateSessionDescriptionMsg* param =
306 static_cast<CreateSessionDescriptionMsg*>(msg->pdata);
Harald Alvestrand73771a82018-05-24 10:53:49 +0200307 param->observer->OnFailure(std::move(param->error));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000308 delete param;
309 break;
310 }
Henrik Boström87713d02015-08-25 09:53:21 +0200311 case MSG_USE_CONSTRUCTOR_CERTIFICATE: {
312 rtc::ScopedRefMessageData<rtc::RTCCertificate>* param =
313 static_cast<rtc::ScopedRefMessageData<rtc::RTCCertificate>*>(
314 msg->pdata);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100315 RTC_LOG(LS_INFO) << "Using certificate supplied to the constructor.";
Henrik Boströmd8281982015-08-27 10:12:24 +0200316 SetCertificate(param->data());
Henrik Boström87713d02015-08-25 09:53:21 +0200317 delete param;
318 break;
319 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000320 default:
nissec80e7412017-01-11 05:56:46 -0800321 RTC_NOTREACHED();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000322 break;
323 }
324}
325
326void WebRtcSessionDescriptionFactory::InternalCreateOffer(
327 CreateSessionDescriptionRequest request) {
Steve Antond5585ca2017-10-23 14:49:26 -0700328 if (pc_->local_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700329 // If the needs-ice-restart flag is set as described by JSEP, we should
330 // generate an offer with a new ufrag/password to trigger an ICE restart.
331 for (cricket::MediaDescriptionOptions& options :
332 request.options.media_description_options) {
Steve Antond5585ca2017-10-23 14:49:26 -0700333 if (pc_->NeedsIceRestart(options.mid)) {
zhihuang1c378ed2017-08-17 14:10:50 -0700334 options.transport_options.ice_restart = true;
deadbeefd1a38b52016-12-10 13:15:33 -0800335 }
336 }
337 }
338
Steve Anton6fe1fba2018-12-11 10:15:23 -0800339 std::unique_ptr<cricket::SessionDescription> desc =
340 session_desc_factory_.CreateOffer(
341 request.options, pc_->local_description()
342 ? pc_->local_description()->description()
343 : nullptr);
344 if (!desc) {
345 PostCreateSessionDescriptionFailed(request.observer,
346 "Failed to initialize the offer.");
347 return;
348 }
349
wu@webrtc.org91053e72013-08-10 07:18:04 +0000350 // RFC 3264
351 // When issuing an offer that modifies the session,
352 // the "o=" line of the new SDP MUST be identical to that in the
353 // previous SDP, except that the version in the origin field MUST
354 // increment by one from the previous SDP.
355
356 // Just increase the version number by one each time when a new offer
357 // is created regardless if it's identical to the previous one or not.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200358 // The |session_version_| is a uint64_t, the wrap around should not happen.
nisseede5da42017-01-12 05:15:36 -0800359 RTC_DCHECK(session_version_ + 1 > session_version_);
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200360 auto offer = std::make_unique<JsepSessionDescription>(
Steve Anton6fe1fba2018-12-11 10:15:23 -0800361 SdpType::kOffer, std::move(desc), session_id_,
362 rtc::ToString(session_version_++));
Steve Antond5585ca2017-10-23 14:49:26 -0700363 if (pc_->local_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700364 for (const cricket::MediaDescriptionOptions& options :
365 request.options.media_description_options) {
366 if (!options.transport_options.ice_restart) {
Steve Antond5585ca2017-10-23 14:49:26 -0700367 CopyCandidatesFromSessionDescription(pc_->local_description(),
Steve Antona3a92c22017-12-07 10:27:41 -0800368 options.mid, offer.get());
deadbeef0ed85b22016-02-23 17:24:52 -0800369 }
370 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000371 }
Steve Antona3a92c22017-12-07 10:27:41 -0800372 PostCreateSessionDescriptionSucceeded(request.observer, std::move(offer));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000373}
374
375void WebRtcSessionDescriptionFactory::InternalCreateAnswer(
376 CreateSessionDescriptionRequest request) {
Steve Antond5585ca2017-10-23 14:49:26 -0700377 if (pc_->remote_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700378 for (cricket::MediaDescriptionOptions& options :
379 request.options.media_description_options) {
deadbeef0ed85b22016-02-23 17:24:52 -0800380 // According to http://tools.ietf.org/html/rfc5245#section-9.2.1.1
381 // an answer should also contain new ICE ufrag and password if an offer
382 // has been received with new ufrag and password.
zhihuang1c378ed2017-08-17 14:10:50 -0700383 options.transport_options.ice_restart =
Steve Antond5585ca2017-10-23 14:49:26 -0700384 pc_->IceRestartPending(options.mid);
deadbeef0ed85b22016-02-23 17:24:52 -0800385 // We should pass the current SSL role to the transport description
386 // factory, if there is already an existing ongoing session.
387 rtc::SSLRole ssl_role;
Steve Antond5585ca2017-10-23 14:49:26 -0700388 if (pc_->GetSslRole(options.mid, &ssl_role)) {
zhihuang1c378ed2017-08-17 14:10:50 -0700389 options.transport_options.prefer_passive_role =
deadbeef0ed85b22016-02-23 17:24:52 -0800390 (rtc::SSL_SERVER == ssl_role);
391 }
392 }
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000393 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000394
Steve Anton6fe1fba2018-12-11 10:15:23 -0800395 std::unique_ptr<cricket::SessionDescription> desc =
396 session_desc_factory_.CreateAnswer(
397 pc_->remote_description() ? pc_->remote_description()->description()
398 : nullptr,
399 request.options,
400 pc_->local_description() ? pc_->local_description()->description()
401 : nullptr);
402 if (!desc) {
403 PostCreateSessionDescriptionFailed(request.observer,
404 "Failed to initialize the answer.");
405 return;
406 }
407
wu@webrtc.org91053e72013-08-10 07:18:04 +0000408 // RFC 3264
409 // If the answer is different from the offer in any way (different IP
410 // addresses, ports, etc.), the origin line MUST be different in the answer.
411 // In that case, the version number in the "o=" line of the answer is
412 // unrelated to the version number in the o line of the offer.
413 // Get a new version number by increasing the |session_version_answer_|.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200414 // The |session_version_| is a uint64_t, the wrap around should not happen.
nisseede5da42017-01-12 05:15:36 -0800415 RTC_DCHECK(session_version_ + 1 > session_version_);
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200416 auto answer = std::make_unique<JsepSessionDescription>(
Steve Anton6fe1fba2018-12-11 10:15:23 -0800417 SdpType::kAnswer, std::move(desc), session_id_,
418 rtc::ToString(session_version_++));
Steve Antond5585ca2017-10-23 14:49:26 -0700419 if (pc_->local_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700420 // Include all local ICE candidates in the SessionDescription unless
421 // the remote peer has requested an ICE restart.
422 for (const cricket::MediaDescriptionOptions& options :
423 request.options.media_description_options) {
424 if (!options.transport_options.ice_restart) {
Steve Antond5585ca2017-10-23 14:49:26 -0700425 CopyCandidatesFromSessionDescription(pc_->local_description(),
Steve Antona3a92c22017-12-07 10:27:41 -0800426 options.mid, answer.get());
deadbeef0ed85b22016-02-23 17:24:52 -0800427 }
428 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000429 }
Steve Antona3a92c22017-12-07 10:27:41 -0800430 PostCreateSessionDescriptionSucceeded(request.observer, std::move(answer));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000431}
432
tommi0f620f42015-07-09 03:25:02 -0700433void WebRtcSessionDescriptionFactory::FailPendingRequests(
434 const std::string& reason) {
nisseede5da42017-01-12 05:15:36 -0800435 RTC_DCHECK(signaling_thread_->IsCurrent());
tommi0f620f42015-07-09 03:25:02 -0700436 while (!create_session_description_requests_.empty()) {
437 const CreateSessionDescriptionRequest& request =
438 create_session_description_requests_.front();
Yves Gerey665174f2018-06-19 15:03:05 +0200439 PostCreateSessionDescriptionFailed(
440 request.observer,
441 ((request.type == CreateSessionDescriptionRequest::kOffer)
442 ? "CreateOffer"
443 : "CreateAnswer") +
444 reason);
tommi0f620f42015-07-09 03:25:02 -0700445 create_session_description_requests_.pop();
446 }
447}
448
wu@webrtc.org91053e72013-08-10 07:18:04 +0000449void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionFailed(
Yves Gerey665174f2018-06-19 15:03:05 +0200450 CreateSessionDescriptionObserver* observer,
451 const std::string& error) {
Harald Alvestrand73771a82018-05-24 10:53:49 +0200452 CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(
453 observer, RTCError(RTCErrorType::INTERNAL_ERROR, std::string(error)));
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700454 signaling_thread_->Post(RTC_FROM_HERE, this,
455 MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100456 RTC_LOG(LS_ERROR) << "Create SDP failed: " << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000457}
458
459void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionSucceeded(
460 CreateSessionDescriptionObserver* observer,
Steve Antona3a92c22017-12-07 10:27:41 -0800461 std::unique_ptr<SessionDescriptionInterface> description) {
Harald Alvestrand73771a82018-05-24 10:53:49 +0200462 CreateSessionDescriptionMsg* msg =
463 new CreateSessionDescriptionMsg(observer, RTCError::OK());
Steve Antona3a92c22017-12-07 10:27:41 -0800464 msg->description = std::move(description);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700465 signaling_thread_->Post(RTC_FROM_HERE, this,
466 MSG_CREATE_SESSIONDESCRIPTION_SUCCESS, msg);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000467}
468
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200469void WebRtcSessionDescriptionFactory::OnCertificateRequestFailed() {
nisseede5da42017-01-12 05:15:36 -0800470 RTC_DCHECK(signaling_thread_->IsCurrent());
wu@webrtc.org91053e72013-08-10 07:18:04 +0000471
Mirko Bonadei675513b2017-11-09 11:09:25 +0100472 RTC_LOG(LS_ERROR) << "Asynchronous certificate generation request failed.";
Henrik Boström87713d02015-08-25 09:53:21 +0200473 certificate_request_state_ = CERTIFICATE_FAILED;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000474
tommi0f620f42015-07-09 03:25:02 -0700475 FailPendingRequests(kFailedDueToIdentityFailed);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000476}
477
Henrik Boströmd8281982015-08-27 10:12:24 +0200478void WebRtcSessionDescriptionFactory::SetCertificate(
479 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
henrikg91d6ede2015-09-17 00:24:34 -0700480 RTC_DCHECK(certificate);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100481 RTC_LOG(LS_VERBOSE) << "Setting new certificate.";
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000482
Henrik Boström87713d02015-08-25 09:53:21 +0200483 certificate_request_state_ = CERTIFICATE_SUCCEEDED;
Henrik Boströmd8281982015-08-27 10:12:24 +0200484 SignalCertificateReady(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000485
Henrik Boström3a14bf32015-08-31 09:27:58 +0200486 transport_desc_factory_.set_certificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000487 transport_desc_factory_.set_secure(cricket::SEC_ENABLED);
488
489 while (!create_session_description_requests_.empty()) {
490 if (create_session_description_requests_.front().type ==
491 CreateSessionDescriptionRequest::kOffer) {
492 InternalCreateOffer(create_session_description_requests_.front());
493 } else {
494 InternalCreateAnswer(create_session_description_requests_.front());
495 }
496 create_session_description_requests_.pop();
497 }
498}
wu@webrtc.org91053e72013-08-10 07:18:04 +0000499} // namespace webrtc