blob: feec1fdd41fabf757523bde6e274081a49a5b011 [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"
Steve Antona3a92c22017-12-07 10:27:41 -080023#include "rtc_base/ptr_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/sslidentity.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000025
wu@webrtc.org364f2042013-11-20 21:49:41 +000026using cricket::MediaSessionOptions;
27
wu@webrtc.org91053e72013-08-10 07:18:04 +000028namespace webrtc {
wu@webrtc.org91053e72013-08-10 07:18:04 +000029namespace {
wu@webrtc.org91053e72013-08-10 07:18:04 +000030static const char kFailedDueToIdentityFailed[] =
31 " failed because DTLS identity request failed";
tommi0f620f42015-07-09 03:25:02 -070032static const char kFailedDueToSessionShutdown[] =
33 " failed because the session was shut down";
wu@webrtc.org91053e72013-08-10 07:18:04 +000034
Peter Boström0c4e06b2015-10-07 12:23:21 +020035static const uint64_t kInitSessionVersion = 2;
wu@webrtc.org91053e72013-08-10 07:18:04 +000036
zhihuang1c378ed2017-08-17 14:10:50 -070037static bool CompareSenderOptions(const cricket::SenderOptions& sender1,
38 const cricket::SenderOptions& sender2) {
39 return sender1.track_id < sender2.track_id;
wu@webrtc.org91053e72013-08-10 07:18:04 +000040}
41
zhihuang1c378ed2017-08-17 14:10:50 -070042static bool SameId(const cricket::SenderOptions& sender1,
43 const cricket::SenderOptions& sender2) {
44 return sender1.track_id == sender2.track_id;
wu@webrtc.org91053e72013-08-10 07:18:04 +000045}
46
zhihuang1c378ed2017-08-17 14:10:50 -070047// Check that each sender has a unique ID.
48static bool ValidMediaSessionOptions(
49 const cricket::MediaSessionOptions& session_options) {
50 std::vector<cricket::SenderOptions> sorted_senders;
51 for (const cricket::MediaDescriptionOptions& media_description_options :
52 session_options.media_description_options) {
53 sorted_senders.insert(sorted_senders.end(),
54 media_description_options.sender_options.begin(),
55 media_description_options.sender_options.end());
56 }
57 std::sort(sorted_senders.begin(), sorted_senders.end(), CompareSenderOptions);
58 std::vector<cricket::SenderOptions>::iterator it =
59 std::adjacent_find(sorted_senders.begin(), sorted_senders.end(), SameId);
60 return it == sorted_senders.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +000061}
62
63enum {
64 MSG_CREATE_SESSIONDESCRIPTION_SUCCESS,
Henrik Boström87713d02015-08-25 09:53:21 +020065 MSG_CREATE_SESSIONDESCRIPTION_FAILED,
66 MSG_USE_CONSTRUCTOR_CERTIFICATE
wu@webrtc.org91053e72013-08-10 07:18:04 +000067};
68
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000069struct CreateSessionDescriptionMsg : public rtc::MessageData {
wu@webrtc.org91053e72013-08-10 07:18:04 +000070 explicit CreateSessionDescriptionMsg(
71 webrtc::CreateSessionDescriptionObserver* observer)
72 : observer(observer) {
73 }
74
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000075 rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer;
wu@webrtc.org91053e72013-08-10 07:18:04 +000076 std::string error;
kwibergd1fe2812016-04-27 06:47:29 -070077 std::unique_ptr<webrtc::SessionDescriptionInterface> description;
wu@webrtc.org91053e72013-08-10 07:18:04 +000078};
wu@webrtc.org91053e72013-08-10 07:18:04 +000079} // namespace
80
Henrik Boströmd03c23b2016-06-01 11:44:18 +020081void WebRtcCertificateGeneratorCallback::OnFailure() {
82 SignalRequestFailed();
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000083}
84
Henrik Boströmd03c23b2016-06-01 11:44:18 +020085void WebRtcCertificateGeneratorCallback::OnSuccess(
86 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
87 SignalCertificateReady(certificate);
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000088}
89
wu@webrtc.org91053e72013-08-10 07:18:04 +000090// static
91void WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
92 const SessionDescriptionInterface* source_desc,
deadbeef0ed85b22016-02-23 17:24:52 -080093 const std::string& content_name,
wu@webrtc.org91053e72013-08-10 07:18:04 +000094 SessionDescriptionInterface* dest_desc) {
deadbeef0ed85b22016-02-23 17:24:52 -080095 if (!source_desc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +000096 return;
deadbeef0ed85b22016-02-23 17:24:52 -080097 }
98 const cricket::ContentInfos& contents =
99 source_desc->description()->contents();
100 const cricket::ContentInfo* cinfo =
101 source_desc->description()->GetContentByName(content_name);
102 if (!cinfo) {
103 return;
104 }
105 size_t mediasection_index = static_cast<int>(cinfo - &contents[0]);
106 const IceCandidateCollection* source_candidates =
107 source_desc->candidates(mediasection_index);
108 const IceCandidateCollection* dest_candidates =
109 dest_desc->candidates(mediasection_index);
Taylor Brandstetter4eb1ddd2016-03-01 16:21:07 -0800110 if (!source_candidates || !dest_candidates) {
111 return;
112 }
deadbeef0ed85b22016-02-23 17:24:52 -0800113 for (size_t n = 0; n < source_candidates->count(); ++n) {
114 const IceCandidateInterface* new_candidate = source_candidates->at(n);
115 if (!dest_candidates->HasCandidate(new_candidate)) {
116 dest_desc->AddCandidate(source_candidates->at(n));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000117 }
118 }
119}
120
Henrik Boström87713d02015-08-25 09:53:21 +0200121// Private constructor called by other constructors.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000122WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000123 rtc::Thread* signaling_thread,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000124 cricket::ChannelManager* channel_manager,
Steve Antond5585ca2017-10-23 14:49:26 -0700125 PeerConnection* pc,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000126 const std::string& session_id,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200127 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
128 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate)
wu@webrtc.org91053e72013-08-10 07:18:04 +0000129 : signaling_thread_(signaling_thread),
wu@webrtc.org91053e72013-08-10 07:18:04 +0000130 session_desc_factory_(channel_manager, &transport_desc_factory_),
131 // RFC 4566 suggested a Network Time Protocol (NTP) format timestamp
132 // as the session id and session version. To simplify, it should be fine
133 // to just use a random number as session id and start version from
134 // |kInitSessionVersion|.
135 session_version_(kInitSessionVersion),
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200136 cert_generator_(std::move(cert_generator)),
Steve Antond5585ca2017-10-23 14:49:26 -0700137 pc_(pc),
wu@webrtc.org91053e72013-08-10 07:18:04 +0000138 session_id_(session_id),
Henrik Boström87713d02015-08-25 09:53:21 +0200139 certificate_request_state_(CERTIFICATE_NOT_NEEDED) {
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200140 RTC_DCHECK(signaling_thread_);
Steve Antond5585ca2017-10-23 14:49:26 -0700141 RTC_DCHECK(!(cert_generator_ && certificate));
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200142 bool dtls_enabled = cert_generator_ || certificate;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000143 // SRTP-SDES is disabled if DTLS is on.
144 SetSdesPolicy(dtls_enabled ? cricket::SEC_DISABLED : cricket::SEC_REQUIRED);
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200145 if (!dtls_enabled) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100146 RTC_LOG(LS_VERBOSE) << "DTLS-SRTP disabled.";
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200147 return;
148 }
149
150 if (certificate) {
151 // Use |certificate|.
152 certificate_request_state_ = CERTIFICATE_WAITING;
153
Mirko Bonadei675513b2017-11-09 11:09:25 +0100154 RTC_LOG(LS_VERBOSE) << "DTLS-SRTP enabled; has certificate parameter.";
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200155 // We already have a certificate but we wait to do |SetIdentity|; if we do
156 // it in the constructor then the caller has not had a chance to connect to
157 // |SignalCertificateReady|.
158 signaling_thread_->Post(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700159 RTC_FROM_HERE, this, MSG_USE_CONSTRUCTOR_CERTIFICATE,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200160 new rtc::ScopedRefMessageData<rtc::RTCCertificate>(certificate));
161 } else {
162 // Generate certificate.
163 certificate_request_state_ = CERTIFICATE_WAITING;
164
165 rtc::scoped_refptr<WebRtcCertificateGeneratorCallback> callback(
166 new rtc::RefCountedObject<WebRtcCertificateGeneratorCallback>());
167 callback->SignalRequestFailed.connect(
168 this, &WebRtcSessionDescriptionFactory::OnCertificateRequestFailed);
169 callback->SignalCertificateReady.connect(
170 this, &WebRtcSessionDescriptionFactory::SetCertificate);
171
172 rtc::KeyParams key_params = rtc::KeyParams();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100173 RTC_LOG(LS_VERBOSE)
174 << "DTLS-SRTP enabled; sending DTLS identity request (key "
175 << "type: " << key_params.type() << ").";
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200176
177 // Request certificate. This happens asynchronously, so that the caller gets
178 // a chance to connect to |SignalCertificateReady|.
Oskar Sundbom36f8f3e2017-11-16 10:54:27 +0100179 cert_generator_->GenerateCertificateAsync(key_params, rtc::nullopt,
180 callback);
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200181 }
Henrik Boström87713d02015-08-25 09:53:21 +0200182}
wu@webrtc.org91053e72013-08-10 07:18:04 +0000183
wu@webrtc.org91053e72013-08-10 07:18:04 +0000184WebRtcSessionDescriptionFactory::~WebRtcSessionDescriptionFactory() {
nisseede5da42017-01-12 05:15:36 -0800185 RTC_DCHECK(signaling_thread_->IsCurrent());
tommi0f620f42015-07-09 03:25:02 -0700186
187 // Fail any requests that were asked for before identity generation completed.
188 FailPendingRequests(kFailedDueToSessionShutdown);
189
190 // Process all pending notifications in the message queue. If we don't do
191 // this, requests will linger and not know they succeeded or failed.
192 rtc::MessageList list;
193 signaling_thread_->Clear(this, rtc::MQID_ANY, &list);
Henrik Boström87713d02015-08-25 09:53:21 +0200194 for (auto& msg : list) {
195 if (msg.message_id != MSG_USE_CONSTRUCTOR_CERTIFICATE) {
196 OnMessage(&msg);
197 } else {
198 // Skip MSG_USE_CONSTRUCTOR_CERTIFICATE because we don't want to trigger
199 // SetIdentity-related callbacks in the destructor. This can be a problem
200 // when WebRtcSession listens to the callback but it was the WebRtcSession
201 // destructor that caused WebRtcSessionDescriptionFactory's destruction.
202 // The callback is then ignored, leaking memory allocated by OnMessage for
203 // MSG_USE_CONSTRUCTOR_CERTIFICATE.
204 delete msg.pdata;
205 }
206 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000207}
208
209void WebRtcSessionDescriptionFactory::CreateOffer(
210 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700211 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
212 const cricket::MediaSessionOptions& session_options) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000213 std::string error = "CreateOffer";
Henrik Boström87713d02015-08-25 09:53:21 +0200214 if (certificate_request_state_ == CERTIFICATE_FAILED) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000215 error += kFailedDueToIdentityFailed;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100216 RTC_LOG(LS_ERROR) << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000217 PostCreateSessionDescriptionFailed(observer, error);
218 return;
219 }
220
zhihuang1c378ed2017-08-17 14:10:50 -0700221 if (!ValidMediaSessionOptions(session_options)) {
222 error += " called with invalid session options";
Mirko Bonadei675513b2017-11-09 11:09:25 +0100223 RTC_LOG(LS_ERROR) << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000224 PostCreateSessionDescriptionFailed(observer, error);
225 return;
226 }
227
wu@webrtc.org91053e72013-08-10 07:18:04 +0000228 CreateSessionDescriptionRequest request(
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000229 CreateSessionDescriptionRequest::kOffer, observer, session_options);
Henrik Boström87713d02015-08-25 09:53:21 +0200230 if (certificate_request_state_ == CERTIFICATE_WAITING) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000231 create_session_description_requests_.push(request);
232 } else {
nisseede5da42017-01-12 05:15:36 -0800233 RTC_DCHECK(certificate_request_state_ == CERTIFICATE_SUCCEEDED ||
234 certificate_request_state_ == CERTIFICATE_NOT_NEEDED);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000235 InternalCreateOffer(request);
236 }
237}
238
239void WebRtcSessionDescriptionFactory::CreateAnswer(
240 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700241 const cricket::MediaSessionOptions& session_options) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000242 std::string error = "CreateAnswer";
Henrik Boström87713d02015-08-25 09:53:21 +0200243 if (certificate_request_state_ == CERTIFICATE_FAILED) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000244 error += kFailedDueToIdentityFailed;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100245 RTC_LOG(LS_ERROR) << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000246 PostCreateSessionDescriptionFailed(observer, error);
247 return;
248 }
Steve Antond5585ca2017-10-23 14:49:26 -0700249 if (!pc_->remote_description()) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000250 error += " can't be called before SetRemoteDescription.";
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 Antona3a92c22017-12-07 10:27:41 -0800255 if (pc_->remote_description()->GetType() != SdpType::kOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000256 error += " failed because remote_description is not an offer.";
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 }
261
zhihuang1c378ed2017-08-17 14:10:50 -0700262 if (!ValidMediaSessionOptions(session_options)) {
263 error += " called with invalid session options.";
Mirko Bonadei675513b2017-11-09 11:09:25 +0100264 RTC_LOG(LS_ERROR) << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000265 PostCreateSessionDescriptionFailed(observer, error);
266 return;
267 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000268
269 CreateSessionDescriptionRequest request(
deadbeefab9b2d12015-10-14 11:33:11 -0700270 CreateSessionDescriptionRequest::kAnswer, observer, session_options);
Henrik Boström87713d02015-08-25 09:53:21 +0200271 if (certificate_request_state_ == CERTIFICATE_WAITING) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000272 create_session_description_requests_.push(request);
273 } else {
nisseede5da42017-01-12 05:15:36 -0800274 RTC_DCHECK(certificate_request_state_ == CERTIFICATE_SUCCEEDED ||
275 certificate_request_state_ == CERTIFICATE_NOT_NEEDED);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000276 InternalCreateAnswer(request);
277 }
278}
279
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000280void WebRtcSessionDescriptionFactory::SetSdesPolicy(
281 cricket::SecurePolicy secure_policy) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000282 session_desc_factory_.set_secure(secure_policy);
283}
284
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000285cricket::SecurePolicy WebRtcSessionDescriptionFactory::SdesPolicy() const {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000286 return session_desc_factory_.secure();
287}
288
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000289void WebRtcSessionDescriptionFactory::OnMessage(rtc::Message* msg) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000290 switch (msg->message_id) {
291 case MSG_CREATE_SESSIONDESCRIPTION_SUCCESS: {
292 CreateSessionDescriptionMsg* param =
293 static_cast<CreateSessionDescriptionMsg*>(msg->pdata);
294 param->observer->OnSuccess(param->description.release());
295 delete param;
296 break;
297 }
298 case MSG_CREATE_SESSIONDESCRIPTION_FAILED: {
299 CreateSessionDescriptionMsg* param =
300 static_cast<CreateSessionDescriptionMsg*>(msg->pdata);
301 param->observer->OnFailure(param->error);
302 delete param;
303 break;
304 }
Henrik Boström87713d02015-08-25 09:53:21 +0200305 case MSG_USE_CONSTRUCTOR_CERTIFICATE: {
306 rtc::ScopedRefMessageData<rtc::RTCCertificate>* param =
307 static_cast<rtc::ScopedRefMessageData<rtc::RTCCertificate>*>(
308 msg->pdata);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100309 RTC_LOG(LS_INFO) << "Using certificate supplied to the constructor.";
Henrik Boströmd8281982015-08-27 10:12:24 +0200310 SetCertificate(param->data());
Henrik Boström87713d02015-08-25 09:53:21 +0200311 delete param;
312 break;
313 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000314 default:
nissec80e7412017-01-11 05:56:46 -0800315 RTC_NOTREACHED();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000316 break;
317 }
318}
319
320void WebRtcSessionDescriptionFactory::InternalCreateOffer(
321 CreateSessionDescriptionRequest request) {
Steve Antond5585ca2017-10-23 14:49:26 -0700322 if (pc_->local_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700323 // If the needs-ice-restart flag is set as described by JSEP, we should
324 // generate an offer with a new ufrag/password to trigger an ICE restart.
325 for (cricket::MediaDescriptionOptions& options :
326 request.options.media_description_options) {
Steve Antond5585ca2017-10-23 14:49:26 -0700327 if (pc_->NeedsIceRestart(options.mid)) {
zhihuang1c378ed2017-08-17 14:10:50 -0700328 options.transport_options.ice_restart = true;
deadbeefd1a38b52016-12-10 13:15:33 -0800329 }
330 }
331 }
332
deadbeefd59daf82015-10-14 15:02:44 -0700333 cricket::SessionDescription* desc(session_desc_factory_.CreateOffer(
Steve Antond5585ca2017-10-23 14:49:26 -0700334 request.options, pc_->local_description()
335 ? pc_->local_description()->description()
deadbeefd59daf82015-10-14 15:02:44 -0700336 : nullptr));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000337 // RFC 3264
338 // When issuing an offer that modifies the session,
339 // the "o=" line of the new SDP MUST be identical to that in the
340 // previous SDP, except that the version in the origin field MUST
341 // increment by one from the previous SDP.
342
343 // Just increase the version number by one each time when a new offer
344 // is created regardless if it's identical to the previous one or not.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200345 // The |session_version_| is a uint64_t, the wrap around should not happen.
nisseede5da42017-01-12 05:15:36 -0800346 RTC_DCHECK(session_version_ + 1 > session_version_);
Steve Antona3a92c22017-12-07 10:27:41 -0800347 auto offer = rtc::MakeUnique<JsepSessionDescription>(SdpType::kOffer);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000348 if (!offer->Initialize(desc, session_id_,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000349 rtc::ToString(session_version_++))) {
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(),
Steve Antona3a92c22017-12-07 10:27:41 -0800359 options.mid, offer.get());
deadbeef0ed85b22016-02-23 17:24:52 -0800360 }
361 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000362 }
Steve Antona3a92c22017-12-07 10:27:41 -0800363 PostCreateSessionDescriptionSucceeded(request.observer, std::move(offer));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000364}
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_);
Steve Antona3a92c22017-12-07 10:27:41 -0800400 auto answer = rtc::MakeUnique<JsepSessionDescription>(SdpType::kAnswer);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000401 if (!answer->Initialize(desc, session_id_,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000402 rtc::ToString(session_version_++))) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000403 PostCreateSessionDescriptionFailed(request.observer,
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000404 "Failed to initialize the answer.");
wu@webrtc.org91053e72013-08-10 07:18:04 +0000405 return;
406 }
Steve Antond5585ca2017-10-23 14:49:26 -0700407 if (pc_->local_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700408 // Include all local ICE candidates in the SessionDescription unless
409 // the remote peer has requested an ICE restart.
410 for (const cricket::MediaDescriptionOptions& options :
411 request.options.media_description_options) {
412 if (!options.transport_options.ice_restart) {
Steve Antond5585ca2017-10-23 14:49:26 -0700413 CopyCandidatesFromSessionDescription(pc_->local_description(),
Steve Antona3a92c22017-12-07 10:27:41 -0800414 options.mid, answer.get());
deadbeef0ed85b22016-02-23 17:24:52 -0800415 }
416 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000417 }
Steve Antona3a92c22017-12-07 10:27:41 -0800418 PostCreateSessionDescriptionSucceeded(request.observer, std::move(answer));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000419}
420
tommi0f620f42015-07-09 03:25:02 -0700421void WebRtcSessionDescriptionFactory::FailPendingRequests(
422 const std::string& reason) {
nisseede5da42017-01-12 05:15:36 -0800423 RTC_DCHECK(signaling_thread_->IsCurrent());
tommi0f620f42015-07-09 03:25:02 -0700424 while (!create_session_description_requests_.empty()) {
425 const CreateSessionDescriptionRequest& request =
426 create_session_description_requests_.front();
427 PostCreateSessionDescriptionFailed(request.observer,
428 ((request.type == CreateSessionDescriptionRequest::kOffer) ?
429 "CreateOffer" : "CreateAnswer") + reason);
430 create_session_description_requests_.pop();
431 }
432}
433
wu@webrtc.org91053e72013-08-10 07:18:04 +0000434void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionFailed(
435 CreateSessionDescriptionObserver* observer, const std::string& error) {
436 CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer);
437 msg->error = error;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700438 signaling_thread_->Post(RTC_FROM_HERE, this,
439 MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100440 RTC_LOG(LS_ERROR) << "Create SDP failed: " << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000441}
442
443void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionSucceeded(
444 CreateSessionDescriptionObserver* observer,
Steve Antona3a92c22017-12-07 10:27:41 -0800445 std::unique_ptr<SessionDescriptionInterface> description) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000446 CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer);
Steve Antona3a92c22017-12-07 10:27:41 -0800447 msg->description = std::move(description);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700448 signaling_thread_->Post(RTC_FROM_HERE, this,
449 MSG_CREATE_SESSIONDESCRIPTION_SUCCESS, msg);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000450}
451
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200452void WebRtcSessionDescriptionFactory::OnCertificateRequestFailed() {
nisseede5da42017-01-12 05:15:36 -0800453 RTC_DCHECK(signaling_thread_->IsCurrent());
wu@webrtc.org91053e72013-08-10 07:18:04 +0000454
Mirko Bonadei675513b2017-11-09 11:09:25 +0100455 RTC_LOG(LS_ERROR) << "Asynchronous certificate generation request failed.";
Henrik Boström87713d02015-08-25 09:53:21 +0200456 certificate_request_state_ = CERTIFICATE_FAILED;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000457
tommi0f620f42015-07-09 03:25:02 -0700458 FailPendingRequests(kFailedDueToIdentityFailed);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000459}
460
Henrik Boströmd8281982015-08-27 10:12:24 +0200461void WebRtcSessionDescriptionFactory::SetCertificate(
462 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
henrikg91d6ede2015-09-17 00:24:34 -0700463 RTC_DCHECK(certificate);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100464 RTC_LOG(LS_VERBOSE) << "Setting new certificate.";
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000465
Henrik Boström87713d02015-08-25 09:53:21 +0200466 certificate_request_state_ = CERTIFICATE_SUCCEEDED;
Henrik Boströmd8281982015-08-27 10:12:24 +0200467 SignalCertificateReady(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000468
Henrik Boström3a14bf32015-08-31 09:27:58 +0200469 transport_desc_factory_.set_certificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000470 transport_desc_factory_.set_secure(cricket::SEC_ENABLED);
471
472 while (!create_session_description_requests_.empty()) {
473 if (create_session_description_requests_.front().type ==
474 CreateSessionDescriptionRequest::kOffer) {
475 InternalCreateOffer(create_session_description_requests_.front());
476 } else {
477 InternalCreateAnswer(create_session_description_requests_.front());
478 }
479 create_session_description_requests_.pop();
480 }
481}
wu@webrtc.org91053e72013-08-10 07:18:04 +0000482} // namespace webrtc