blob: 18e9ba03fde2ac6c170138f59d6ee4a78b76147b [file] [log] [blame]
wu@webrtc.org91053e72013-08-10 07:18:04 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
wu@webrtc.org91053e72013-08-10 07:18:04 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
wu@webrtc.org91053e72013-08-10 07:18:04 +00009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/webrtcsessiondescriptionfactory.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000012
Steve Anton36b29d12017-10-30 09:57:42 -070013#include <algorithm>
14#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080015#include <utility>
Steve Anton36b29d12017-10-30 09:57:42 -070016#include <vector>
kwiberg0eb15ed2015-12-17 03:04:15 -080017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/jsep.h"
19#include "api/jsepsessiondescription.h"
20#include "api/mediaconstraintsinterface.h"
Steve Antond5585ca2017-10-23 14:49:26 -070021#include "pc/peerconnection.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/checks.h"
23#include "rtc_base/sslidentity.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000024
wu@webrtc.org364f2042013-11-20 21:49:41 +000025using cricket::MediaSessionOptions;
26
wu@webrtc.org91053e72013-08-10 07:18:04 +000027namespace webrtc {
wu@webrtc.org91053e72013-08-10 07:18:04 +000028namespace {
wu@webrtc.org91053e72013-08-10 07:18:04 +000029static const char kFailedDueToIdentityFailed[] =
30 " failed because DTLS identity request failed";
tommi0f620f42015-07-09 03:25:02 -070031static const char kFailedDueToSessionShutdown[] =
32 " failed because the session was shut down";
wu@webrtc.org91053e72013-08-10 07:18:04 +000033
Peter Boström0c4e06b2015-10-07 12:23:21 +020034static const uint64_t kInitSessionVersion = 2;
wu@webrtc.org91053e72013-08-10 07:18:04 +000035
zhihuang1c378ed2017-08-17 14:10:50 -070036static bool CompareSenderOptions(const cricket::SenderOptions& sender1,
37 const cricket::SenderOptions& sender2) {
38 return sender1.track_id < sender2.track_id;
wu@webrtc.org91053e72013-08-10 07:18:04 +000039}
40
zhihuang1c378ed2017-08-17 14:10:50 -070041static bool SameId(const cricket::SenderOptions& sender1,
42 const cricket::SenderOptions& sender2) {
43 return sender1.track_id == sender2.track_id;
wu@webrtc.org91053e72013-08-10 07:18:04 +000044}
45
zhihuang1c378ed2017-08-17 14:10:50 -070046// Check that each sender has a unique ID.
47static bool ValidMediaSessionOptions(
48 const cricket::MediaSessionOptions& session_options) {
49 std::vector<cricket::SenderOptions> sorted_senders;
50 for (const cricket::MediaDescriptionOptions& media_description_options :
51 session_options.media_description_options) {
52 sorted_senders.insert(sorted_senders.end(),
53 media_description_options.sender_options.begin(),
54 media_description_options.sender_options.end());
55 }
56 std::sort(sorted_senders.begin(), sorted_senders.end(), CompareSenderOptions);
57 std::vector<cricket::SenderOptions>::iterator it =
58 std::adjacent_find(sorted_senders.begin(), sorted_senders.end(), SameId);
59 return it == sorted_senders.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +000060}
61
62enum {
63 MSG_CREATE_SESSIONDESCRIPTION_SUCCESS,
Henrik Boström87713d02015-08-25 09:53:21 +020064 MSG_CREATE_SESSIONDESCRIPTION_FAILED,
65 MSG_USE_CONSTRUCTOR_CERTIFICATE
wu@webrtc.org91053e72013-08-10 07:18:04 +000066};
67
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000068struct CreateSessionDescriptionMsg : public rtc::MessageData {
wu@webrtc.org91053e72013-08-10 07:18:04 +000069 explicit CreateSessionDescriptionMsg(
70 webrtc::CreateSessionDescriptionObserver* observer)
71 : observer(observer) {
72 }
73
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000074 rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer;
wu@webrtc.org91053e72013-08-10 07:18:04 +000075 std::string error;
kwibergd1fe2812016-04-27 06:47:29 -070076 std::unique_ptr<webrtc::SessionDescriptionInterface> description;
wu@webrtc.org91053e72013-08-10 07:18:04 +000077};
wu@webrtc.org91053e72013-08-10 07:18:04 +000078} // namespace
79
Henrik Boströmd03c23b2016-06-01 11:44:18 +020080void WebRtcCertificateGeneratorCallback::OnFailure() {
81 SignalRequestFailed();
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000082}
83
Henrik Boströmd03c23b2016-06-01 11:44:18 +020084void WebRtcCertificateGeneratorCallback::OnSuccess(
85 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
86 SignalCertificateReady(certificate);
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000087}
88
wu@webrtc.org91053e72013-08-10 07:18:04 +000089// static
90void WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
91 const SessionDescriptionInterface* source_desc,
deadbeef0ed85b22016-02-23 17:24:52 -080092 const std::string& content_name,
wu@webrtc.org91053e72013-08-10 07:18:04 +000093 SessionDescriptionInterface* dest_desc) {
deadbeef0ed85b22016-02-23 17:24:52 -080094 if (!source_desc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +000095 return;
deadbeef0ed85b22016-02-23 17:24:52 -080096 }
97 const cricket::ContentInfos& contents =
98 source_desc->description()->contents();
99 const cricket::ContentInfo* cinfo =
100 source_desc->description()->GetContentByName(content_name);
101 if (!cinfo) {
102 return;
103 }
104 size_t mediasection_index = static_cast<int>(cinfo - &contents[0]);
105 const IceCandidateCollection* source_candidates =
106 source_desc->candidates(mediasection_index);
107 const IceCandidateCollection* dest_candidates =
108 dest_desc->candidates(mediasection_index);
Taylor Brandstetter4eb1ddd2016-03-01 16:21:07 -0800109 if (!source_candidates || !dest_candidates) {
110 return;
111 }
deadbeef0ed85b22016-02-23 17:24:52 -0800112 for (size_t n = 0; n < source_candidates->count(); ++n) {
113 const IceCandidateInterface* new_candidate = source_candidates->at(n);
114 if (!dest_candidates->HasCandidate(new_candidate)) {
115 dest_desc->AddCandidate(source_candidates->at(n));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000116 }
117 }
118}
119
Henrik Boström87713d02015-08-25 09:53:21 +0200120// Private constructor called by other constructors.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000121WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000122 rtc::Thread* signaling_thread,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000123 cricket::ChannelManager* channel_manager,
Steve Antond5585ca2017-10-23 14:49:26 -0700124 PeerConnection* pc,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000125 const std::string& session_id,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200126 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
127 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate)
wu@webrtc.org91053e72013-08-10 07:18:04 +0000128 : signaling_thread_(signaling_thread),
wu@webrtc.org91053e72013-08-10 07:18:04 +0000129 session_desc_factory_(channel_manager, &transport_desc_factory_),
130 // RFC 4566 suggested a Network Time Protocol (NTP) format timestamp
131 // as the session id and session version. To simplify, it should be fine
132 // to just use a random number as session id and start version from
133 // |kInitSessionVersion|.
134 session_version_(kInitSessionVersion),
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200135 cert_generator_(std::move(cert_generator)),
Steve Antond5585ca2017-10-23 14:49:26 -0700136 pc_(pc),
wu@webrtc.org91053e72013-08-10 07:18:04 +0000137 session_id_(session_id),
Henrik Boström87713d02015-08-25 09:53:21 +0200138 certificate_request_state_(CERTIFICATE_NOT_NEEDED) {
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200139 RTC_DCHECK(signaling_thread_);
Steve Antond5585ca2017-10-23 14:49:26 -0700140 RTC_DCHECK(!(cert_generator_ && certificate));
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200141 bool dtls_enabled = cert_generator_ || certificate;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000142 // SRTP-SDES is disabled if DTLS is on.
143 SetSdesPolicy(dtls_enabled ? cricket::SEC_DISABLED : cricket::SEC_REQUIRED);
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200144 if (!dtls_enabled) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100145 RTC_LOG(LS_VERBOSE) << "DTLS-SRTP disabled.";
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200146 return;
147 }
148
149 if (certificate) {
150 // Use |certificate|.
151 certificate_request_state_ = CERTIFICATE_WAITING;
152
Mirko Bonadei675513b2017-11-09 11:09:25 +0100153 RTC_LOG(LS_VERBOSE) << "DTLS-SRTP enabled; has certificate parameter.";
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200154 // We already have a certificate but we wait to do |SetIdentity|; if we do
155 // it in the constructor then the caller has not had a chance to connect to
156 // |SignalCertificateReady|.
157 signaling_thread_->Post(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700158 RTC_FROM_HERE, this, MSG_USE_CONSTRUCTOR_CERTIFICATE,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200159 new rtc::ScopedRefMessageData<rtc::RTCCertificate>(certificate));
160 } else {
161 // Generate certificate.
162 certificate_request_state_ = CERTIFICATE_WAITING;
163
164 rtc::scoped_refptr<WebRtcCertificateGeneratorCallback> callback(
165 new rtc::RefCountedObject<WebRtcCertificateGeneratorCallback>());
166 callback->SignalRequestFailed.connect(
167 this, &WebRtcSessionDescriptionFactory::OnCertificateRequestFailed);
168 callback->SignalCertificateReady.connect(
169 this, &WebRtcSessionDescriptionFactory::SetCertificate);
170
171 rtc::KeyParams key_params = rtc::KeyParams();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100172 RTC_LOG(LS_VERBOSE)
173 << "DTLS-SRTP enabled; sending DTLS identity request (key "
174 << "type: " << key_params.type() << ").";
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200175
176 // Request certificate. This happens asynchronously, so that the caller gets
177 // a chance to connect to |SignalCertificateReady|.
Oskar Sundbom36f8f3e2017-11-16 10:54:27 +0100178 cert_generator_->GenerateCertificateAsync(key_params, rtc::nullopt,
179 callback);
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200180 }
Henrik Boström87713d02015-08-25 09:53:21 +0200181}
wu@webrtc.org91053e72013-08-10 07:18:04 +0000182
wu@webrtc.org91053e72013-08-10 07:18:04 +0000183WebRtcSessionDescriptionFactory::~WebRtcSessionDescriptionFactory() {
nisseede5da42017-01-12 05:15:36 -0800184 RTC_DCHECK(signaling_thread_->IsCurrent());
tommi0f620f42015-07-09 03:25:02 -0700185
186 // Fail any requests that were asked for before identity generation completed.
187 FailPendingRequests(kFailedDueToSessionShutdown);
188
189 // Process all pending notifications in the message queue. If we don't do
190 // this, requests will linger and not know they succeeded or failed.
191 rtc::MessageList list;
192 signaling_thread_->Clear(this, rtc::MQID_ANY, &list);
Henrik Boström87713d02015-08-25 09:53:21 +0200193 for (auto& msg : list) {
194 if (msg.message_id != MSG_USE_CONSTRUCTOR_CERTIFICATE) {
195 OnMessage(&msg);
196 } else {
197 // Skip MSG_USE_CONSTRUCTOR_CERTIFICATE because we don't want to trigger
198 // SetIdentity-related callbacks in the destructor. This can be a problem
199 // when WebRtcSession listens to the callback but it was the WebRtcSession
200 // destructor that caused WebRtcSessionDescriptionFactory's destruction.
201 // The callback is then ignored, leaking memory allocated by OnMessage for
202 // MSG_USE_CONSTRUCTOR_CERTIFICATE.
203 delete msg.pdata;
204 }
205 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000206}
207
208void WebRtcSessionDescriptionFactory::CreateOffer(
209 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700210 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
211 const cricket::MediaSessionOptions& session_options) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000212 std::string error = "CreateOffer";
Henrik Boström87713d02015-08-25 09:53:21 +0200213 if (certificate_request_state_ == CERTIFICATE_FAILED) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000214 error += kFailedDueToIdentityFailed;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100215 RTC_LOG(LS_ERROR) << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000216 PostCreateSessionDescriptionFailed(observer, error);
217 return;
218 }
219
zhihuang1c378ed2017-08-17 14:10:50 -0700220 if (!ValidMediaSessionOptions(session_options)) {
221 error += " called with invalid session options";
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
wu@webrtc.org91053e72013-08-10 07:18:04 +0000227 CreateSessionDescriptionRequest request(
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000228 CreateSessionDescriptionRequest::kOffer, observer, session_options);
Henrik Boström87713d02015-08-25 09:53:21 +0200229 if (certificate_request_state_ == CERTIFICATE_WAITING) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000230 create_session_description_requests_.push(request);
231 } else {
nisseede5da42017-01-12 05:15:36 -0800232 RTC_DCHECK(certificate_request_state_ == CERTIFICATE_SUCCEEDED ||
233 certificate_request_state_ == CERTIFICATE_NOT_NEEDED);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000234 InternalCreateOffer(request);
235 }
236}
237
238void WebRtcSessionDescriptionFactory::CreateAnswer(
239 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700240 const cricket::MediaSessionOptions& session_options) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000241 std::string error = "CreateAnswer";
Henrik Boström87713d02015-08-25 09:53:21 +0200242 if (certificate_request_state_ == CERTIFICATE_FAILED) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000243 error += kFailedDueToIdentityFailed;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100244 RTC_LOG(LS_ERROR) << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000245 PostCreateSessionDescriptionFailed(observer, error);
246 return;
247 }
Steve Antond5585ca2017-10-23 14:49:26 -0700248 if (!pc_->remote_description()) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000249 error += " can't be called before SetRemoteDescription.";
Mirko Bonadei675513b2017-11-09 11:09:25 +0100250 RTC_LOG(LS_ERROR) << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000251 PostCreateSessionDescriptionFailed(observer, error);
252 return;
253 }
Steve Antond5585ca2017-10-23 14:49:26 -0700254 if (pc_->remote_description()->type() != JsepSessionDescription::kOffer) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000255 error += " failed because remote_description is not an offer.";
Mirko Bonadei675513b2017-11-09 11:09:25 +0100256 RTC_LOG(LS_ERROR) << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000257 PostCreateSessionDescriptionFailed(observer, error);
258 return;
259 }
260
zhihuang1c378ed2017-08-17 14:10:50 -0700261 if (!ValidMediaSessionOptions(session_options)) {
262 error += " called with invalid session options.";
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 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000267
268 CreateSessionDescriptionRequest request(
deadbeefab9b2d12015-10-14 11:33:11 -0700269 CreateSessionDescriptionRequest::kAnswer, observer, session_options);
Henrik Boström87713d02015-08-25 09:53:21 +0200270 if (certificate_request_state_ == CERTIFICATE_WAITING) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000271 create_session_description_requests_.push(request);
272 } else {
nisseede5da42017-01-12 05:15:36 -0800273 RTC_DCHECK(certificate_request_state_ == CERTIFICATE_SUCCEEDED ||
274 certificate_request_state_ == CERTIFICATE_NOT_NEEDED);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000275 InternalCreateAnswer(request);
276 }
277}
278
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000279void WebRtcSessionDescriptionFactory::SetSdesPolicy(
280 cricket::SecurePolicy secure_policy) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000281 session_desc_factory_.set_secure(secure_policy);
282}
283
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000284cricket::SecurePolicy WebRtcSessionDescriptionFactory::SdesPolicy() const {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000285 return session_desc_factory_.secure();
286}
287
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000288void WebRtcSessionDescriptionFactory::OnMessage(rtc::Message* msg) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000289 switch (msg->message_id) {
290 case MSG_CREATE_SESSIONDESCRIPTION_SUCCESS: {
291 CreateSessionDescriptionMsg* param =
292 static_cast<CreateSessionDescriptionMsg*>(msg->pdata);
293 param->observer->OnSuccess(param->description.release());
294 delete param;
295 break;
296 }
297 case MSG_CREATE_SESSIONDESCRIPTION_FAILED: {
298 CreateSessionDescriptionMsg* param =
299 static_cast<CreateSessionDescriptionMsg*>(msg->pdata);
300 param->observer->OnFailure(param->error);
301 delete param;
302 break;
303 }
Henrik Boström87713d02015-08-25 09:53:21 +0200304 case MSG_USE_CONSTRUCTOR_CERTIFICATE: {
305 rtc::ScopedRefMessageData<rtc::RTCCertificate>* param =
306 static_cast<rtc::ScopedRefMessageData<rtc::RTCCertificate>*>(
307 msg->pdata);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100308 RTC_LOG(LS_INFO) << "Using certificate supplied to the constructor.";
Henrik Boströmd8281982015-08-27 10:12:24 +0200309 SetCertificate(param->data());
Henrik Boström87713d02015-08-25 09:53:21 +0200310 delete param;
311 break;
312 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000313 default:
nissec80e7412017-01-11 05:56:46 -0800314 RTC_NOTREACHED();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000315 break;
316 }
317}
318
319void WebRtcSessionDescriptionFactory::InternalCreateOffer(
320 CreateSessionDescriptionRequest request) {
Steve Antond5585ca2017-10-23 14:49:26 -0700321 if (pc_->local_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700322 // If the needs-ice-restart flag is set as described by JSEP, we should
323 // generate an offer with a new ufrag/password to trigger an ICE restart.
324 for (cricket::MediaDescriptionOptions& options :
325 request.options.media_description_options) {
Steve Antond5585ca2017-10-23 14:49:26 -0700326 if (pc_->NeedsIceRestart(options.mid)) {
zhihuang1c378ed2017-08-17 14:10:50 -0700327 options.transport_options.ice_restart = true;
deadbeefd1a38b52016-12-10 13:15:33 -0800328 }
329 }
330 }
331
deadbeefd59daf82015-10-14 15:02:44 -0700332 cricket::SessionDescription* desc(session_desc_factory_.CreateOffer(
Steve Antond5585ca2017-10-23 14:49:26 -0700333 request.options, pc_->local_description()
334 ? pc_->local_description()->description()
deadbeefd59daf82015-10-14 15:02:44 -0700335 : nullptr));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000336 // RFC 3264
337 // When issuing an offer that modifies the session,
338 // the "o=" line of the new SDP MUST be identical to that in the
339 // previous SDP, except that the version in the origin field MUST
340 // increment by one from the previous SDP.
341
342 // Just increase the version number by one each time when a new offer
343 // is created regardless if it's identical to the previous one or not.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200344 // The |session_version_| is a uint64_t, the wrap around should not happen.
nisseede5da42017-01-12 05:15:36 -0800345 RTC_DCHECK(session_version_ + 1 > session_version_);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000346 JsepSessionDescription* offer(new JsepSessionDescription(
347 JsepSessionDescription::kOffer));
348 if (!offer->Initialize(desc, session_id_,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000349 rtc::ToString(session_version_++))) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000350 delete offer;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000351 PostCreateSessionDescriptionFailed(request.observer,
352 "Failed to initialize the offer.");
wu@webrtc.org91053e72013-08-10 07:18:04 +0000353 return;
354 }
Steve Antond5585ca2017-10-23 14:49:26 -0700355 if (pc_->local_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700356 for (const cricket::MediaDescriptionOptions& options :
357 request.options.media_description_options) {
358 if (!options.transport_options.ice_restart) {
Steve Antond5585ca2017-10-23 14:49:26 -0700359 CopyCandidatesFromSessionDescription(pc_->local_description(),
zhihuang1c378ed2017-08-17 14:10:50 -0700360 options.mid, offer);
deadbeef0ed85b22016-02-23 17:24:52 -0800361 }
362 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000363 }
364 PostCreateSessionDescriptionSucceeded(request.observer, offer);
365}
366
367void WebRtcSessionDescriptionFactory::InternalCreateAnswer(
368 CreateSessionDescriptionRequest request) {
Steve Antond5585ca2017-10-23 14:49:26 -0700369 if (pc_->remote_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700370 for (cricket::MediaDescriptionOptions& options :
371 request.options.media_description_options) {
deadbeef0ed85b22016-02-23 17:24:52 -0800372 // According to http://tools.ietf.org/html/rfc5245#section-9.2.1.1
373 // an answer should also contain new ICE ufrag and password if an offer
374 // has been received with new ufrag and password.
zhihuang1c378ed2017-08-17 14:10:50 -0700375 options.transport_options.ice_restart =
Steve Antond5585ca2017-10-23 14:49:26 -0700376 pc_->IceRestartPending(options.mid);
deadbeef0ed85b22016-02-23 17:24:52 -0800377 // We should pass the current SSL role to the transport description
378 // factory, if there is already an existing ongoing session.
379 rtc::SSLRole ssl_role;
Steve Antond5585ca2017-10-23 14:49:26 -0700380 if (pc_->GetSslRole(options.mid, &ssl_role)) {
zhihuang1c378ed2017-08-17 14:10:50 -0700381 options.transport_options.prefer_passive_role =
deadbeef0ed85b22016-02-23 17:24:52 -0800382 (rtc::SSL_SERVER == ssl_role);
383 }
384 }
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000385 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000386
387 cricket::SessionDescription* desc(session_desc_factory_.CreateAnswer(
Steve Antond5585ca2017-10-23 14:49:26 -0700388 pc_->remote_description() ? pc_->remote_description()->description()
389 : nullptr,
390 request.options,
391 pc_->local_description() ? pc_->local_description()->description()
392 : nullptr));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000393 // RFC 3264
394 // If the answer is different from the offer in any way (different IP
395 // addresses, ports, etc.), the origin line MUST be different in the answer.
396 // In that case, the version number in the "o=" line of the answer is
397 // unrelated to the version number in the o line of the offer.
398 // Get a new version number by increasing the |session_version_answer_|.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200399 // The |session_version_| is a uint64_t, the wrap around should not happen.
nisseede5da42017-01-12 05:15:36 -0800400 RTC_DCHECK(session_version_ + 1 > session_version_);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000401 JsepSessionDescription* answer(new JsepSessionDescription(
402 JsepSessionDescription::kAnswer));
403 if (!answer->Initialize(desc, session_id_,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000404 rtc::ToString(session_version_++))) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000405 delete answer;
406 PostCreateSessionDescriptionFailed(request.observer,
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000407 "Failed to initialize the answer.");
wu@webrtc.org91053e72013-08-10 07:18:04 +0000408 return;
409 }
Steve Antond5585ca2017-10-23 14:49:26 -0700410 if (pc_->local_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700411 // Include all local ICE candidates in the SessionDescription unless
412 // the remote peer has requested an ICE restart.
413 for (const cricket::MediaDescriptionOptions& options :
414 request.options.media_description_options) {
415 if (!options.transport_options.ice_restart) {
Steve Antond5585ca2017-10-23 14:49:26 -0700416 CopyCandidatesFromSessionDescription(pc_->local_description(),
zhihuang1c378ed2017-08-17 14:10:50 -0700417 options.mid, answer);
deadbeef0ed85b22016-02-23 17:24:52 -0800418 }
419 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000420 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000421 PostCreateSessionDescriptionSucceeded(request.observer, answer);
422}
423
tommi0f620f42015-07-09 03:25:02 -0700424void WebRtcSessionDescriptionFactory::FailPendingRequests(
425 const std::string& reason) {
nisseede5da42017-01-12 05:15:36 -0800426 RTC_DCHECK(signaling_thread_->IsCurrent());
tommi0f620f42015-07-09 03:25:02 -0700427 while (!create_session_description_requests_.empty()) {
428 const CreateSessionDescriptionRequest& request =
429 create_session_description_requests_.front();
430 PostCreateSessionDescriptionFailed(request.observer,
431 ((request.type == CreateSessionDescriptionRequest::kOffer) ?
432 "CreateOffer" : "CreateAnswer") + reason);
433 create_session_description_requests_.pop();
434 }
435}
436
wu@webrtc.org91053e72013-08-10 07:18:04 +0000437void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionFailed(
438 CreateSessionDescriptionObserver* observer, const std::string& error) {
439 CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer);
440 msg->error = error;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700441 signaling_thread_->Post(RTC_FROM_HERE, this,
442 MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100443 RTC_LOG(LS_ERROR) << "Create SDP failed: " << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000444}
445
446void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionSucceeded(
447 CreateSessionDescriptionObserver* observer,
448 SessionDescriptionInterface* description) {
449 CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer);
450 msg->description.reset(description);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700451 signaling_thread_->Post(RTC_FROM_HERE, this,
452 MSG_CREATE_SESSIONDESCRIPTION_SUCCESS, msg);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000453}
454
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200455void WebRtcSessionDescriptionFactory::OnCertificateRequestFailed() {
nisseede5da42017-01-12 05:15:36 -0800456 RTC_DCHECK(signaling_thread_->IsCurrent());
wu@webrtc.org91053e72013-08-10 07:18:04 +0000457
Mirko Bonadei675513b2017-11-09 11:09:25 +0100458 RTC_LOG(LS_ERROR) << "Asynchronous certificate generation request failed.";
Henrik Boström87713d02015-08-25 09:53:21 +0200459 certificate_request_state_ = CERTIFICATE_FAILED;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000460
tommi0f620f42015-07-09 03:25:02 -0700461 FailPendingRequests(kFailedDueToIdentityFailed);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000462}
463
Henrik Boströmd8281982015-08-27 10:12:24 +0200464void WebRtcSessionDescriptionFactory::SetCertificate(
465 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
henrikg91d6ede2015-09-17 00:24:34 -0700466 RTC_DCHECK(certificate);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100467 RTC_LOG(LS_VERBOSE) << "Setting new certificate.";
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000468
Henrik Boström87713d02015-08-25 09:53:21 +0200469 certificate_request_state_ = CERTIFICATE_SUCCEEDED;
Henrik Boströmd8281982015-08-27 10:12:24 +0200470 SignalCertificateReady(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000471
Henrik Boström3a14bf32015-08-31 09:27:58 +0200472 transport_desc_factory_.set_certificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000473 transport_desc_factory_.set_secure(cricket::SEC_ENABLED);
474
475 while (!create_session_description_requests_.empty()) {
476 if (create_session_description_requests_.front().type ==
477 CreateSessionDescriptionRequest::kOffer) {
478 InternalCreateOffer(create_session_description_requests_.front());
479 } else {
480 InternalCreateAnswer(create_session_description_requests_.front());
481 }
482 create_session_description_requests_.pop();
483 }
484}
wu@webrtc.org91053e72013-08-10 07:18:04 +0000485} // namespace webrtc