blob: bfdbb1ac0a091ff87cf3663c93867b3c9cf95dd1 [file] [log] [blame]
wu@webrtc.org91053e72013-08-10 07:18:04 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2013 Google Inc.
wu@webrtc.org91053e72013-08-10 07:18:04 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/app/webrtc/webrtcsessiondescriptionfactory.h"
29
kwiberg0eb15ed2015-12-17 03:04:15 -080030#include <utility>
31
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000032#include "talk/app/webrtc/dtlsidentitystore.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000033#include "talk/app/webrtc/jsep.h"
34#include "talk/app/webrtc/jsepsessiondescription.h"
35#include "talk/app/webrtc/mediaconstraintsinterface.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000036#include "talk/app/webrtc/webrtcsession.h"
Henrik Boström5e56c592015-08-11 10:33:13 +020037#include "webrtc/base/sslidentity.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000038
wu@webrtc.org364f2042013-11-20 21:49:41 +000039using cricket::MediaSessionOptions;
40
wu@webrtc.org91053e72013-08-10 07:18:04 +000041namespace webrtc {
wu@webrtc.org91053e72013-08-10 07:18:04 +000042namespace {
wu@webrtc.org91053e72013-08-10 07:18:04 +000043static const char kFailedDueToIdentityFailed[] =
44 " failed because DTLS identity request failed";
tommi0f620f42015-07-09 03:25:02 -070045static const char kFailedDueToSessionShutdown[] =
46 " failed because the session was shut down";
wu@webrtc.org91053e72013-08-10 07:18:04 +000047
Peter Boström0c4e06b2015-10-07 12:23:21 +020048static const uint64_t kInitSessionVersion = 2;
wu@webrtc.org91053e72013-08-10 07:18:04 +000049
wu@webrtc.org364f2042013-11-20 21:49:41 +000050static bool CompareStream(const MediaSessionOptions::Stream& stream1,
51 const MediaSessionOptions::Stream& stream2) {
52 return stream1.id < stream2.id;
wu@webrtc.org91053e72013-08-10 07:18:04 +000053}
54
wu@webrtc.org364f2042013-11-20 21:49:41 +000055static bool SameId(const MediaSessionOptions::Stream& stream1,
56 const MediaSessionOptions::Stream& stream2) {
57 return stream1.id == stream2.id;
wu@webrtc.org91053e72013-08-10 07:18:04 +000058}
59
60// Checks if each Stream within the |streams| has unique id.
wu@webrtc.org364f2042013-11-20 21:49:41 +000061static bool ValidStreams(const MediaSessionOptions::Streams& streams) {
62 MediaSessionOptions::Streams sorted_streams = streams;
wu@webrtc.org91053e72013-08-10 07:18:04 +000063 std::sort(sorted_streams.begin(), sorted_streams.end(), CompareStream);
wu@webrtc.org364f2042013-11-20 21:49:41 +000064 MediaSessionOptions::Streams::iterator it =
wu@webrtc.org91053e72013-08-10 07:18:04 +000065 std::adjacent_find(sorted_streams.begin(), sorted_streams.end(),
66 SameId);
wu@webrtc.org364f2042013-11-20 21:49:41 +000067 return it == sorted_streams.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +000068}
69
70enum {
71 MSG_CREATE_SESSIONDESCRIPTION_SUCCESS,
Henrik Boström87713d02015-08-25 09:53:21 +020072 MSG_CREATE_SESSIONDESCRIPTION_FAILED,
73 MSG_USE_CONSTRUCTOR_CERTIFICATE
wu@webrtc.org91053e72013-08-10 07:18:04 +000074};
75
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000076struct CreateSessionDescriptionMsg : public rtc::MessageData {
wu@webrtc.org91053e72013-08-10 07:18:04 +000077 explicit CreateSessionDescriptionMsg(
78 webrtc::CreateSessionDescriptionObserver* observer)
79 : observer(observer) {
80 }
81
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000082 rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer;
wu@webrtc.org91053e72013-08-10 07:18:04 +000083 std::string error;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000084 rtc::scoped_ptr<webrtc::SessionDescriptionInterface> description;
wu@webrtc.org91053e72013-08-10 07:18:04 +000085};
wu@webrtc.org91053e72013-08-10 07:18:04 +000086} // namespace
87
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000088void WebRtcIdentityRequestObserver::OnFailure(int error) {
89 SignalRequestFailed(error);
90}
91
92void WebRtcIdentityRequestObserver::OnSuccess(
93 const std::string& der_cert, const std::string& der_private_key) {
94 std::string pem_cert = rtc::SSLIdentity::DerToPem(
95 rtc::kPemTypeCertificate,
96 reinterpret_cast<const unsigned char*>(der_cert.data()),
97 der_cert.length());
98 std::string pem_key = rtc::SSLIdentity::DerToPem(
99 rtc::kPemTypeRsaPrivateKey,
100 reinterpret_cast<const unsigned char*>(der_private_key.data()),
101 der_private_key.length());
Henrik Boströmd8281982015-08-27 10:12:24 +0200102 rtc::scoped_ptr<rtc::SSLIdentity> identity(
103 rtc::SSLIdentity::FromPEMStrings(pem_key, pem_cert));
kwiberg0eb15ed2015-12-17 03:04:15 -0800104 SignalCertificateReady(rtc::RTCCertificate::Create(std::move(identity)));
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000105}
106
Henrik Boström5e56c592015-08-11 10:33:13 +0200107void WebRtcIdentityRequestObserver::OnSuccess(
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000108 rtc::scoped_ptr<rtc::SSLIdentity> identity) {
kwiberg0eb15ed2015-12-17 03:04:15 -0800109 SignalCertificateReady(rtc::RTCCertificate::Create(std::move(identity)));
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000110}
111
wu@webrtc.org91053e72013-08-10 07:18:04 +0000112// static
113void WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
114 const SessionDescriptionInterface* source_desc,
115 SessionDescriptionInterface* dest_desc) {
116 if (!source_desc)
117 return;
118 for (size_t m = 0; m < source_desc->number_of_mediasections() &&
119 m < dest_desc->number_of_mediasections(); ++m) {
120 const IceCandidateCollection* source_candidates =
121 source_desc->candidates(m);
122 const IceCandidateCollection* dest_candidates = dest_desc->candidates(m);
123 for (size_t n = 0; n < source_candidates->count(); ++n) {
124 const IceCandidateInterface* new_candidate = source_candidates->at(n);
125 if (!dest_candidates->HasCandidate(new_candidate))
126 dest_desc->AddCandidate(source_candidates->at(n));
127 }
128 }
129}
130
Henrik Boström87713d02015-08-25 09:53:21 +0200131// Private constructor called by other constructors.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000132WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000133 rtc::Thread* signaling_thread,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000134 cricket::ChannelManager* channel_manager,
Henrik Boström5e56c592015-08-11 10:33:13 +0200135 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
Henrik Boström87713d02015-08-25 09:53:21 +0200136 const rtc::scoped_refptr<WebRtcIdentityRequestObserver>&
137 identity_request_observer,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000138 WebRtcSession* session,
139 const std::string& session_id,
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000140 bool dtls_enabled)
wu@webrtc.org91053e72013-08-10 07:18:04 +0000141 : signaling_thread_(signaling_thread),
wu@webrtc.org91053e72013-08-10 07:18:04 +0000142 session_desc_factory_(channel_manager, &transport_desc_factory_),
143 // RFC 4566 suggested a Network Time Protocol (NTP) format timestamp
144 // as the session id and session version. To simplify, it should be fine
145 // to just use a random number as session id and start version from
146 // |kInitSessionVersion|.
147 session_version_(kInitSessionVersion),
kwiberg0eb15ed2015-12-17 03:04:15 -0800148 dtls_identity_store_(std::move(dtls_identity_store)),
Henrik Boström87713d02015-08-25 09:53:21 +0200149 identity_request_observer_(identity_request_observer),
wu@webrtc.org91053e72013-08-10 07:18:04 +0000150 session_(session),
151 session_id_(session_id),
Henrik Boström87713d02015-08-25 09:53:21 +0200152 certificate_request_state_(CERTIFICATE_NOT_NEEDED) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000153 session_desc_factory_.set_add_legacy_streams(false);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000154 // SRTP-SDES is disabled if DTLS is on.
155 SetSdesPolicy(dtls_enabled ? cricket::SEC_DISABLED : cricket::SEC_REQUIRED);
Henrik Boström87713d02015-08-25 09:53:21 +0200156}
wu@webrtc.org91053e72013-08-10 07:18:04 +0000157
Henrik Boström87713d02015-08-25 09:53:21 +0200158WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory(
159 rtc::Thread* signaling_thread,
160 cricket::ChannelManager* channel_manager,
Henrik Boström87713d02015-08-25 09:53:21 +0200161 WebRtcSession* session,
deadbeefab9b2d12015-10-14 11:33:11 -0700162 const std::string& session_id)
deadbeefcbecd352015-09-23 11:50:27 -0700163 : WebRtcSessionDescriptionFactory(signaling_thread,
164 channel_manager,
deadbeefcbecd352015-09-23 11:50:27 -0700165 nullptr,
166 nullptr,
167 session,
168 session_id,
deadbeefcbecd352015-09-23 11:50:27 -0700169 false) {
Henrik Boström87713d02015-08-25 09:53:21 +0200170 LOG(LS_VERBOSE) << "DTLS-SRTP disabled.";
171}
wu@webrtc.org91053e72013-08-10 07:18:04 +0000172
Henrik Boström87713d02015-08-25 09:53:21 +0200173WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory(
174 rtc::Thread* signaling_thread,
175 cricket::ChannelManager* channel_manager,
Henrik Boström87713d02015-08-25 09:53:21 +0200176 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
177 WebRtcSession* session,
deadbeefab9b2d12015-10-14 11:33:11 -0700178 const std::string& session_id)
Henrik Boström87713d02015-08-25 09:53:21 +0200179 : WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700180 signaling_thread,
181 channel_manager,
kwiberg0eb15ed2015-12-17 03:04:15 -0800182 std::move(dtls_identity_store),
deadbeefab9b2d12015-10-14 11:33:11 -0700183 new rtc::RefCountedObject<WebRtcIdentityRequestObserver>(),
184 session,
185 session_id,
186 true) {
henrikg91d6ede2015-09-17 00:24:34 -0700187 RTC_DCHECK(dtls_identity_store_);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000188
Henrik Boström87713d02015-08-25 09:53:21 +0200189 certificate_request_state_ = CERTIFICATE_WAITING;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000190
Henrik Boström87713d02015-08-25 09:53:21 +0200191 identity_request_observer_->SignalRequestFailed.connect(
192 this, &WebRtcSessionDescriptionFactory::OnIdentityRequestFailed);
Henrik Boströmd8281982015-08-27 10:12:24 +0200193 identity_request_observer_->SignalCertificateReady.connect(
194 this, &WebRtcSessionDescriptionFactory::SetCertificate);
Henrik Boström87713d02015-08-25 09:53:21 +0200195
196 rtc::KeyType key_type = rtc::KT_DEFAULT;
197 LOG(LS_VERBOSE) << "DTLS-SRTP enabled; sending DTLS identity request (key "
198 << "type: " << key_type << ").";
199
200 // Request identity. This happens asynchronously, so the caller will have a
201 // chance to connect to SignalIdentityReady.
202 dtls_identity_store_->RequestIdentity(key_type, identity_request_observer_);
203}
204
205WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory(
206 rtc::Thread* signaling_thread,
207 cricket::ChannelManager* channel_manager,
Henrik Boström87713d02015-08-25 09:53:21 +0200208 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate,
209 WebRtcSession* session,
deadbeefab9b2d12015-10-14 11:33:11 -0700210 const std::string& session_id)
211 : WebRtcSessionDescriptionFactory(signaling_thread,
212 channel_manager,
213 nullptr,
214 nullptr,
215 session,
216 session_id,
217 true) {
henrikg91d6ede2015-09-17 00:24:34 -0700218 RTC_DCHECK(certificate);
Henrik Boström87713d02015-08-25 09:53:21 +0200219
220 certificate_request_state_ = CERTIFICATE_WAITING;
221
222 LOG(LS_VERBOSE) << "DTLS-SRTP enabled; has certificate parameter.";
223 // We already have a certificate but we wait to do SetIdentity; if we do
224 // it in the constructor then the caller has not had a chance to connect to
225 // SignalIdentityReady.
deadbeefcbecd352015-09-23 11:50:27 -0700226 signaling_thread_->Post(
227 this, MSG_USE_CONSTRUCTOR_CERTIFICATE,
228 new rtc::ScopedRefMessageData<rtc::RTCCertificate>(certificate));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000229}
230
231WebRtcSessionDescriptionFactory::~WebRtcSessionDescriptionFactory() {
tommi0f620f42015-07-09 03:25:02 -0700232 ASSERT(signaling_thread_->IsCurrent());
233
234 // Fail any requests that were asked for before identity generation completed.
235 FailPendingRequests(kFailedDueToSessionShutdown);
236
237 // Process all pending notifications in the message queue. If we don't do
238 // this, requests will linger and not know they succeeded or failed.
239 rtc::MessageList list;
240 signaling_thread_->Clear(this, rtc::MQID_ANY, &list);
Henrik Boström87713d02015-08-25 09:53:21 +0200241 for (auto& msg : list) {
242 if (msg.message_id != MSG_USE_CONSTRUCTOR_CERTIFICATE) {
243 OnMessage(&msg);
244 } else {
245 // Skip MSG_USE_CONSTRUCTOR_CERTIFICATE because we don't want to trigger
246 // SetIdentity-related callbacks in the destructor. This can be a problem
247 // when WebRtcSession listens to the callback but it was the WebRtcSession
248 // destructor that caused WebRtcSessionDescriptionFactory's destruction.
249 // The callback is then ignored, leaking memory allocated by OnMessage for
250 // MSG_USE_CONSTRUCTOR_CERTIFICATE.
251 delete msg.pdata;
252 }
253 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000254}
255
256void WebRtcSessionDescriptionFactory::CreateOffer(
257 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700258 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
259 const cricket::MediaSessionOptions& session_options) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000260 std::string error = "CreateOffer";
Henrik Boström87713d02015-08-25 09:53:21 +0200261 if (certificate_request_state_ == CERTIFICATE_FAILED) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000262 error += kFailedDueToIdentityFailed;
263 LOG(LS_ERROR) << error;
264 PostCreateSessionDescriptionFailed(observer, error);
265 return;
266 }
267
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000268 if (!ValidStreams(session_options.streams)) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000269 error += " called with invalid media streams.";
270 LOG(LS_ERROR) << error;
271 PostCreateSessionDescriptionFailed(observer, error);
272 return;
273 }
274
wu@webrtc.org91053e72013-08-10 07:18:04 +0000275 CreateSessionDescriptionRequest request(
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000276 CreateSessionDescriptionRequest::kOffer, 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 {
Henrik Boström87713d02015-08-25 09:53:21 +0200280 ASSERT(certificate_request_state_ == CERTIFICATE_SUCCEEDED ||
281 certificate_request_state_ == CERTIFICATE_NOT_NEEDED);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000282 InternalCreateOffer(request);
283 }
284}
285
286void WebRtcSessionDescriptionFactory::CreateAnswer(
287 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700288 const MediaConstraintsInterface* constraints,
289 const cricket::MediaSessionOptions& session_options) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000290 std::string error = "CreateAnswer";
Henrik Boström87713d02015-08-25 09:53:21 +0200291 if (certificate_request_state_ == CERTIFICATE_FAILED) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000292 error += kFailedDueToIdentityFailed;
293 LOG(LS_ERROR) << error;
294 PostCreateSessionDescriptionFailed(observer, error);
295 return;
296 }
297 if (!session_->remote_description()) {
298 error += " can't be called before SetRemoteDescription.";
299 LOG(LS_ERROR) << error;
300 PostCreateSessionDescriptionFailed(observer, error);
301 return;
302 }
303 if (session_->remote_description()->type() !=
304 JsepSessionDescription::kOffer) {
305 error += " failed because remote_description is not an offer.";
306 LOG(LS_ERROR) << error;
307 PostCreateSessionDescriptionFailed(observer, error);
308 return;
309 }
310
deadbeefab9b2d12015-10-14 11:33:11 -0700311 if (!ValidStreams(session_options.streams)) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000312 error += " called with invalid media streams.";
313 LOG(LS_ERROR) << error;
314 PostCreateSessionDescriptionFailed(observer, error);
315 return;
316 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000317
318 CreateSessionDescriptionRequest request(
deadbeefab9b2d12015-10-14 11:33:11 -0700319 CreateSessionDescriptionRequest::kAnswer, observer, session_options);
Henrik Boström87713d02015-08-25 09:53:21 +0200320 if (certificate_request_state_ == CERTIFICATE_WAITING) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000321 create_session_description_requests_.push(request);
322 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200323 ASSERT(certificate_request_state_ == CERTIFICATE_SUCCEEDED ||
324 certificate_request_state_ == CERTIFICATE_NOT_NEEDED);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000325 InternalCreateAnswer(request);
326 }
327}
328
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000329void WebRtcSessionDescriptionFactory::SetSdesPolicy(
330 cricket::SecurePolicy secure_policy) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000331 session_desc_factory_.set_secure(secure_policy);
332}
333
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000334cricket::SecurePolicy WebRtcSessionDescriptionFactory::SdesPolicy() const {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000335 return session_desc_factory_.secure();
336}
337
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000338void WebRtcSessionDescriptionFactory::OnMessage(rtc::Message* msg) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000339 switch (msg->message_id) {
340 case MSG_CREATE_SESSIONDESCRIPTION_SUCCESS: {
341 CreateSessionDescriptionMsg* param =
342 static_cast<CreateSessionDescriptionMsg*>(msg->pdata);
343 param->observer->OnSuccess(param->description.release());
344 delete param;
345 break;
346 }
347 case MSG_CREATE_SESSIONDESCRIPTION_FAILED: {
348 CreateSessionDescriptionMsg* param =
349 static_cast<CreateSessionDescriptionMsg*>(msg->pdata);
350 param->observer->OnFailure(param->error);
351 delete param;
352 break;
353 }
Henrik Boström87713d02015-08-25 09:53:21 +0200354 case MSG_USE_CONSTRUCTOR_CERTIFICATE: {
355 rtc::ScopedRefMessageData<rtc::RTCCertificate>* param =
356 static_cast<rtc::ScopedRefMessageData<rtc::RTCCertificate>*>(
357 msg->pdata);
358 LOG(LS_INFO) << "Using certificate supplied to the constructor.";
Henrik Boströmd8281982015-08-27 10:12:24 +0200359 SetCertificate(param->data());
Henrik Boström87713d02015-08-25 09:53:21 +0200360 delete param;
361 break;
362 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000363 default:
364 ASSERT(false);
365 break;
366 }
367}
368
369void WebRtcSessionDescriptionFactory::InternalCreateOffer(
370 CreateSessionDescriptionRequest request) {
deadbeefd59daf82015-10-14 15:02:44 -0700371 cricket::SessionDescription* desc(session_desc_factory_.CreateOffer(
372 request.options, session_->local_description()
373 ? session_->local_description()->description()
374 : nullptr));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000375 // RFC 3264
376 // When issuing an offer that modifies the session,
377 // the "o=" line of the new SDP MUST be identical to that in the
378 // previous SDP, except that the version in the origin field MUST
379 // increment by one from the previous SDP.
380
381 // Just increase the version number by one each time when a new offer
382 // is created regardless if it's identical to the previous one or not.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200383 // The |session_version_| is a uint64_t, the wrap around should not happen.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000384 ASSERT(session_version_ + 1 > session_version_);
385 JsepSessionDescription* offer(new JsepSessionDescription(
386 JsepSessionDescription::kOffer));
387 if (!offer->Initialize(desc, session_id_,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000388 rtc::ToString(session_version_++))) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000389 delete offer;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000390 PostCreateSessionDescriptionFailed(request.observer,
391 "Failed to initialize the offer.");
wu@webrtc.org91053e72013-08-10 07:18:04 +0000392 return;
393 }
394 if (session_->local_description() &&
395 !request.options.transport_options.ice_restart) {
396 // Include all local ice candidates in the SessionDescription unless
397 // the an ice restart has been requested.
398 CopyCandidatesFromSessionDescription(session_->local_description(), offer);
399 }
400 PostCreateSessionDescriptionSucceeded(request.observer, offer);
401}
402
403void WebRtcSessionDescriptionFactory::InternalCreateAnswer(
404 CreateSessionDescriptionRequest request) {
405 // According to http://tools.ietf.org/html/rfc5245#section-9.2.1.1
406 // an answer should also contain new ice ufrag and password if an offer has
407 // been received with new ufrag and password.
408 request.options.transport_options.ice_restart = session_->IceRestartPending();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000409 // We should pass current ssl role to the transport description factory, if
410 // there is already an existing ongoing session.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000411 rtc::SSLRole ssl_role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000412 if (session_->GetSslRole(&ssl_role)) {
413 request.options.transport_options.prefer_passive_role =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000414 (rtc::SSL_SERVER == ssl_role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000415 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000416
417 cricket::SessionDescription* desc(session_desc_factory_.CreateAnswer(
deadbeefd59daf82015-10-14 15:02:44 -0700418 session_->remote_description()
419 ? session_->remote_description()->description()
420 : nullptr,
421 request.options, session_->local_description()
422 ? session_->local_description()->description()
423 : nullptr));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000424 // RFC 3264
425 // If the answer is different from the offer in any way (different IP
426 // addresses, ports, etc.), the origin line MUST be different in the answer.
427 // In that case, the version number in the "o=" line of the answer is
428 // unrelated to the version number in the o line of the offer.
429 // Get a new version number by increasing the |session_version_answer_|.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200430 // The |session_version_| is a uint64_t, the wrap around should not happen.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000431 ASSERT(session_version_ + 1 > session_version_);
432 JsepSessionDescription* answer(new JsepSessionDescription(
433 JsepSessionDescription::kAnswer));
434 if (!answer->Initialize(desc, session_id_,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000435 rtc::ToString(session_version_++))) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000436 delete answer;
437 PostCreateSessionDescriptionFailed(request.observer,
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000438 "Failed to initialize the answer.");
wu@webrtc.org91053e72013-08-10 07:18:04 +0000439 return;
440 }
441 if (session_->local_description() &&
442 !request.options.transport_options.ice_restart) {
443 // Include all local ice candidates in the SessionDescription unless
444 // the remote peer has requested an ice restart.
445 CopyCandidatesFromSessionDescription(session_->local_description(), answer);
446 }
447 session_->ResetIceRestartLatch();
448 PostCreateSessionDescriptionSucceeded(request.observer, answer);
449}
450
tommi0f620f42015-07-09 03:25:02 -0700451void WebRtcSessionDescriptionFactory::FailPendingRequests(
452 const std::string& reason) {
453 ASSERT(signaling_thread_->IsCurrent());
454 while (!create_session_description_requests_.empty()) {
455 const CreateSessionDescriptionRequest& request =
456 create_session_description_requests_.front();
457 PostCreateSessionDescriptionFailed(request.observer,
458 ((request.type == CreateSessionDescriptionRequest::kOffer) ?
459 "CreateOffer" : "CreateAnswer") + reason);
460 create_session_description_requests_.pop();
461 }
462}
463
wu@webrtc.org91053e72013-08-10 07:18:04 +0000464void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionFailed(
465 CreateSessionDescriptionObserver* observer, const std::string& error) {
466 CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer);
467 msg->error = error;
468 signaling_thread_->Post(this, MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000469 LOG(LS_ERROR) << "Create SDP failed: " << error;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000470}
471
472void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionSucceeded(
473 CreateSessionDescriptionObserver* observer,
474 SessionDescriptionInterface* description) {
475 CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer);
476 msg->description.reset(description);
477 signaling_thread_->Post(this, MSG_CREATE_SESSIONDESCRIPTION_SUCCESS, msg);
478}
479
480void WebRtcSessionDescriptionFactory::OnIdentityRequestFailed(int error) {
481 ASSERT(signaling_thread_->IsCurrent());
482
483 LOG(LS_ERROR) << "Async identity request failed: error = " << error;
Henrik Boström87713d02015-08-25 09:53:21 +0200484 certificate_request_state_ = CERTIFICATE_FAILED;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000485
tommi0f620f42015-07-09 03:25:02 -0700486 FailPendingRequests(kFailedDueToIdentityFailed);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000487}
488
Henrik Boströmd8281982015-08-27 10:12:24 +0200489void WebRtcSessionDescriptionFactory::SetCertificate(
490 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
henrikg91d6ede2015-09-17 00:24:34 -0700491 RTC_DCHECK(certificate);
Henrik Boströmd8281982015-08-27 10:12:24 +0200492 LOG(LS_VERBOSE) << "Setting new certificate";
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000493
Henrik Boström87713d02015-08-25 09:53:21 +0200494 certificate_request_state_ = CERTIFICATE_SUCCEEDED;
Henrik Boströmd8281982015-08-27 10:12:24 +0200495 SignalCertificateReady(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000496
Henrik Boström3a14bf32015-08-31 09:27:58 +0200497 transport_desc_factory_.set_certificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000498 transport_desc_factory_.set_secure(cricket::SEC_ENABLED);
499
500 while (!create_session_description_requests_.empty()) {
501 if (create_session_description_requests_.front().type ==
502 CreateSessionDescriptionRequest::kOffer) {
503 InternalCreateOffer(create_session_description_requests_.front());
504 } else {
505 InternalCreateAnswer(create_session_description_requests_.front());
506 }
507 create_session_description_requests_.pop();
508 }
509}
wu@webrtc.org91053e72013-08-10 07:18:04 +0000510} // namespace webrtc