wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * 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.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 11 | #include "webrtc/api/webrtcsessiondescriptionfactory.h" |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 12 | |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 13 | #include <utility> |
| 14 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 15 | #include "webrtc/api/jsep.h" |
| 16 | #include "webrtc/api/jsepsessiondescription.h" |
| 17 | #include "webrtc/api/mediaconstraintsinterface.h" |
| 18 | #include "webrtc/api/webrtcsession.h" |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 19 | #include "webrtc/base/sslidentity.h" |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 20 | |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 21 | using cricket::MediaSessionOptions; |
| 22 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 23 | namespace webrtc { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 24 | namespace { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 25 | static const char kFailedDueToIdentityFailed[] = |
| 26 | " failed because DTLS identity request failed"; |
tommi | 0f620f4 | 2015-07-09 03:25:02 -0700 | [diff] [blame] | 27 | static const char kFailedDueToSessionShutdown[] = |
| 28 | " failed because the session was shut down"; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 29 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 30 | static const uint64_t kInitSessionVersion = 2; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 31 | |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 32 | static bool CompareStream(const MediaSessionOptions::Stream& stream1, |
| 33 | const MediaSessionOptions::Stream& stream2) { |
| 34 | return stream1.id < stream2.id; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 35 | } |
| 36 | |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 37 | static bool SameId(const MediaSessionOptions::Stream& stream1, |
| 38 | const MediaSessionOptions::Stream& stream2) { |
| 39 | return stream1.id == stream2.id; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | // Checks if each Stream within the |streams| has unique id. |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 43 | static bool ValidStreams(const MediaSessionOptions::Streams& streams) { |
| 44 | MediaSessionOptions::Streams sorted_streams = streams; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 45 | std::sort(sorted_streams.begin(), sorted_streams.end(), CompareStream); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 46 | MediaSessionOptions::Streams::iterator it = |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 47 | std::adjacent_find(sorted_streams.begin(), sorted_streams.end(), |
| 48 | SameId); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 49 | return it == sorted_streams.end(); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | enum { |
| 53 | MSG_CREATE_SESSIONDESCRIPTION_SUCCESS, |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 54 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, |
| 55 | MSG_USE_CONSTRUCTOR_CERTIFICATE |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 58 | struct CreateSessionDescriptionMsg : public rtc::MessageData { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 59 | explicit CreateSessionDescriptionMsg( |
| 60 | webrtc::CreateSessionDescriptionObserver* observer) |
| 61 | : observer(observer) { |
| 62 | } |
| 63 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 64 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 65 | std::string error; |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 66 | std::unique_ptr<webrtc::SessionDescriptionInterface> description; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 67 | }; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 68 | } // namespace |
| 69 | |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 70 | void WebRtcCertificateGeneratorCallback::OnFailure() { |
| 71 | SignalRequestFailed(); |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 74 | void WebRtcCertificateGeneratorCallback::OnSuccess( |
| 75 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
| 76 | SignalCertificateReady(certificate); |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 77 | } |
| 78 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 79 | // static |
| 80 | void WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
| 81 | const SessionDescriptionInterface* source_desc, |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 82 | const std::string& content_name, |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 83 | SessionDescriptionInterface* dest_desc) { |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 84 | if (!source_desc) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 85 | return; |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 86 | } |
| 87 | const cricket::ContentInfos& contents = |
| 88 | source_desc->description()->contents(); |
| 89 | const cricket::ContentInfo* cinfo = |
| 90 | source_desc->description()->GetContentByName(content_name); |
| 91 | if (!cinfo) { |
| 92 | return; |
| 93 | } |
| 94 | size_t mediasection_index = static_cast<int>(cinfo - &contents[0]); |
| 95 | const IceCandidateCollection* source_candidates = |
| 96 | source_desc->candidates(mediasection_index); |
| 97 | const IceCandidateCollection* dest_candidates = |
| 98 | dest_desc->candidates(mediasection_index); |
Taylor Brandstetter | 4eb1ddd | 2016-03-01 16:21:07 -0800 | [diff] [blame] | 99 | if (!source_candidates || !dest_candidates) { |
| 100 | return; |
| 101 | } |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 102 | for (size_t n = 0; n < source_candidates->count(); ++n) { |
| 103 | const IceCandidateInterface* new_candidate = source_candidates->at(n); |
| 104 | if (!dest_candidates->HasCandidate(new_candidate)) { |
| 105 | dest_desc->AddCandidate(source_candidates->at(n)); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 110 | // Private constructor called by other constructors. |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 111 | WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 112 | rtc::Thread* signaling_thread, |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 113 | cricket::ChannelManager* channel_manager, |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 114 | WebRtcSession* session, |
| 115 | const std::string& session_id, |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 116 | std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator, |
| 117 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 118 | : signaling_thread_(signaling_thread), |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 119 | session_desc_factory_(channel_manager, &transport_desc_factory_), |
| 120 | // RFC 4566 suggested a Network Time Protocol (NTP) format timestamp |
| 121 | // as the session id and session version. To simplify, it should be fine |
| 122 | // to just use a random number as session id and start version from |
| 123 | // |kInitSessionVersion|. |
| 124 | session_version_(kInitSessionVersion), |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 125 | cert_generator_(std::move(cert_generator)), |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 126 | session_(session), |
| 127 | session_id_(session_id), |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 128 | certificate_request_state_(CERTIFICATE_NOT_NEEDED) { |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 129 | RTC_DCHECK(signaling_thread_); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 130 | session_desc_factory_.set_add_legacy_streams(false); |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 131 | bool dtls_enabled = cert_generator_ || certificate; |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 132 | // SRTP-SDES is disabled if DTLS is on. |
| 133 | SetSdesPolicy(dtls_enabled ? cricket::SEC_DISABLED : cricket::SEC_REQUIRED); |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 134 | if (!dtls_enabled) { |
| 135 | LOG(LS_VERBOSE) << "DTLS-SRTP disabled."; |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | if (certificate) { |
| 140 | // Use |certificate|. |
| 141 | certificate_request_state_ = CERTIFICATE_WAITING; |
| 142 | |
| 143 | LOG(LS_VERBOSE) << "DTLS-SRTP enabled; has certificate parameter."; |
| 144 | // We already have a certificate but we wait to do |SetIdentity|; if we do |
| 145 | // it in the constructor then the caller has not had a chance to connect to |
| 146 | // |SignalCertificateReady|. |
| 147 | signaling_thread_->Post( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame^] | 148 | RTC_FROM_HERE, this, MSG_USE_CONSTRUCTOR_CERTIFICATE, |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 149 | new rtc::ScopedRefMessageData<rtc::RTCCertificate>(certificate)); |
| 150 | } else { |
| 151 | // Generate certificate. |
| 152 | certificate_request_state_ = CERTIFICATE_WAITING; |
| 153 | |
| 154 | rtc::scoped_refptr<WebRtcCertificateGeneratorCallback> callback( |
| 155 | new rtc::RefCountedObject<WebRtcCertificateGeneratorCallback>()); |
| 156 | callback->SignalRequestFailed.connect( |
| 157 | this, &WebRtcSessionDescriptionFactory::OnCertificateRequestFailed); |
| 158 | callback->SignalCertificateReady.connect( |
| 159 | this, &WebRtcSessionDescriptionFactory::SetCertificate); |
| 160 | |
| 161 | rtc::KeyParams key_params = rtc::KeyParams(); |
| 162 | LOG(LS_VERBOSE) << "DTLS-SRTP enabled; sending DTLS identity request (key " |
| 163 | << "type: " << key_params.type() << ")."; |
| 164 | |
| 165 | // Request certificate. This happens asynchronously, so that the caller gets |
| 166 | // a chance to connect to |SignalCertificateReady|. |
| 167 | cert_generator_->GenerateCertificateAsync( |
| 168 | key_params, rtc::Optional<uint64_t>(), callback); |
| 169 | } |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 170 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 171 | |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 172 | WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory( |
| 173 | rtc::Thread* signaling_thread, |
| 174 | cricket::ChannelManager* channel_manager, |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 175 | WebRtcSession* session, |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 176 | const std::string& session_id, |
| 177 | std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator) |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 178 | : WebRtcSessionDescriptionFactory( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 179 | signaling_thread, |
| 180 | channel_manager, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 181 | session, |
| 182 | session_id, |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 183 | std::move(cert_generator), |
| 184 | nullptr) { |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory( |
| 188 | rtc::Thread* signaling_thread, |
| 189 | cricket::ChannelManager* channel_manager, |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 190 | WebRtcSession* session, |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 191 | const std::string& session_id, |
| 192 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 193 | : WebRtcSessionDescriptionFactory(signaling_thread, |
| 194 | channel_manager, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 195 | session, |
| 196 | session_id, |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 197 | nullptr, |
| 198 | certificate) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 199 | RTC_DCHECK(certificate); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | WebRtcSessionDescriptionFactory::~WebRtcSessionDescriptionFactory() { |
tommi | 0f620f4 | 2015-07-09 03:25:02 -0700 | [diff] [blame] | 203 | ASSERT(signaling_thread_->IsCurrent()); |
| 204 | |
| 205 | // Fail any requests that were asked for before identity generation completed. |
| 206 | FailPendingRequests(kFailedDueToSessionShutdown); |
| 207 | |
| 208 | // Process all pending notifications in the message queue. If we don't do |
| 209 | // this, requests will linger and not know they succeeded or failed. |
| 210 | rtc::MessageList list; |
| 211 | signaling_thread_->Clear(this, rtc::MQID_ANY, &list); |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 212 | for (auto& msg : list) { |
| 213 | if (msg.message_id != MSG_USE_CONSTRUCTOR_CERTIFICATE) { |
| 214 | OnMessage(&msg); |
| 215 | } else { |
| 216 | // Skip MSG_USE_CONSTRUCTOR_CERTIFICATE because we don't want to trigger |
| 217 | // SetIdentity-related callbacks in the destructor. This can be a problem |
| 218 | // when WebRtcSession listens to the callback but it was the WebRtcSession |
| 219 | // destructor that caused WebRtcSessionDescriptionFactory's destruction. |
| 220 | // The callback is then ignored, leaking memory allocated by OnMessage for |
| 221 | // MSG_USE_CONSTRUCTOR_CERTIFICATE. |
| 222 | delete msg.pdata; |
| 223 | } |
| 224 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | void WebRtcSessionDescriptionFactory::CreateOffer( |
| 228 | CreateSessionDescriptionObserver* observer, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 229 | const PeerConnectionInterface::RTCOfferAnswerOptions& options, |
| 230 | const cricket::MediaSessionOptions& session_options) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 231 | std::string error = "CreateOffer"; |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 232 | if (certificate_request_state_ == CERTIFICATE_FAILED) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 233 | error += kFailedDueToIdentityFailed; |
| 234 | LOG(LS_ERROR) << error; |
| 235 | PostCreateSessionDescriptionFailed(observer, error); |
| 236 | return; |
| 237 | } |
| 238 | |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 239 | if (!ValidStreams(session_options.streams)) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 240 | error += " called with invalid media streams."; |
| 241 | LOG(LS_ERROR) << error; |
| 242 | PostCreateSessionDescriptionFailed(observer, error); |
| 243 | return; |
| 244 | } |
| 245 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 246 | CreateSessionDescriptionRequest request( |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 247 | CreateSessionDescriptionRequest::kOffer, observer, session_options); |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 248 | if (certificate_request_state_ == CERTIFICATE_WAITING) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 249 | create_session_description_requests_.push(request); |
| 250 | } else { |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 251 | ASSERT(certificate_request_state_ == CERTIFICATE_SUCCEEDED || |
| 252 | certificate_request_state_ == CERTIFICATE_NOT_NEEDED); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 253 | InternalCreateOffer(request); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | void WebRtcSessionDescriptionFactory::CreateAnswer( |
| 258 | CreateSessionDescriptionObserver* observer, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 259 | const cricket::MediaSessionOptions& session_options) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 260 | std::string error = "CreateAnswer"; |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 261 | if (certificate_request_state_ == CERTIFICATE_FAILED) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 262 | error += kFailedDueToIdentityFailed; |
| 263 | LOG(LS_ERROR) << error; |
| 264 | PostCreateSessionDescriptionFailed(observer, error); |
| 265 | return; |
| 266 | } |
| 267 | if (!session_->remote_description()) { |
| 268 | error += " can't be called before SetRemoteDescription."; |
| 269 | LOG(LS_ERROR) << error; |
| 270 | PostCreateSessionDescriptionFailed(observer, error); |
| 271 | return; |
| 272 | } |
| 273 | if (session_->remote_description()->type() != |
| 274 | JsepSessionDescription::kOffer) { |
| 275 | error += " failed because remote_description is not an offer."; |
| 276 | LOG(LS_ERROR) << error; |
| 277 | PostCreateSessionDescriptionFailed(observer, error); |
| 278 | return; |
| 279 | } |
| 280 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 281 | if (!ValidStreams(session_options.streams)) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 282 | error += " called with invalid media streams."; |
| 283 | LOG(LS_ERROR) << error; |
| 284 | PostCreateSessionDescriptionFailed(observer, error); |
| 285 | return; |
| 286 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 287 | |
| 288 | CreateSessionDescriptionRequest request( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 289 | CreateSessionDescriptionRequest::kAnswer, observer, session_options); |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 290 | if (certificate_request_state_ == CERTIFICATE_WAITING) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 291 | create_session_description_requests_.push(request); |
| 292 | } else { |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 293 | ASSERT(certificate_request_state_ == CERTIFICATE_SUCCEEDED || |
| 294 | certificate_request_state_ == CERTIFICATE_NOT_NEEDED); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 295 | InternalCreateAnswer(request); |
| 296 | } |
| 297 | } |
| 298 | |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 299 | void WebRtcSessionDescriptionFactory::SetSdesPolicy( |
| 300 | cricket::SecurePolicy secure_policy) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 301 | session_desc_factory_.set_secure(secure_policy); |
| 302 | } |
| 303 | |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 304 | cricket::SecurePolicy WebRtcSessionDescriptionFactory::SdesPolicy() const { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 305 | return session_desc_factory_.secure(); |
| 306 | } |
| 307 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 308 | void WebRtcSessionDescriptionFactory::OnMessage(rtc::Message* msg) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 309 | switch (msg->message_id) { |
| 310 | case MSG_CREATE_SESSIONDESCRIPTION_SUCCESS: { |
| 311 | CreateSessionDescriptionMsg* param = |
| 312 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 313 | param->observer->OnSuccess(param->description.release()); |
| 314 | delete param; |
| 315 | break; |
| 316 | } |
| 317 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 318 | CreateSessionDescriptionMsg* param = |
| 319 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 320 | param->observer->OnFailure(param->error); |
| 321 | delete param; |
| 322 | break; |
| 323 | } |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 324 | case MSG_USE_CONSTRUCTOR_CERTIFICATE: { |
| 325 | rtc::ScopedRefMessageData<rtc::RTCCertificate>* param = |
| 326 | static_cast<rtc::ScopedRefMessageData<rtc::RTCCertificate>*>( |
| 327 | msg->pdata); |
| 328 | LOG(LS_INFO) << "Using certificate supplied to the constructor."; |
Henrik Boström | d828198 | 2015-08-27 10:12:24 +0200 | [diff] [blame] | 329 | SetCertificate(param->data()); |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 330 | delete param; |
| 331 | break; |
| 332 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 333 | default: |
| 334 | ASSERT(false); |
| 335 | break; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | void WebRtcSessionDescriptionFactory::InternalCreateOffer( |
| 340 | CreateSessionDescriptionRequest request) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 341 | cricket::SessionDescription* desc(session_desc_factory_.CreateOffer( |
| 342 | request.options, session_->local_description() |
| 343 | ? session_->local_description()->description() |
| 344 | : nullptr)); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 345 | // RFC 3264 |
| 346 | // When issuing an offer that modifies the session, |
| 347 | // the "o=" line of the new SDP MUST be identical to that in the |
| 348 | // previous SDP, except that the version in the origin field MUST |
| 349 | // increment by one from the previous SDP. |
| 350 | |
| 351 | // Just increase the version number by one each time when a new offer |
| 352 | // is created regardless if it's identical to the previous one or not. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 353 | // The |session_version_| is a uint64_t, the wrap around should not happen. |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 354 | ASSERT(session_version_ + 1 > session_version_); |
| 355 | JsepSessionDescription* offer(new JsepSessionDescription( |
| 356 | JsepSessionDescription::kOffer)); |
| 357 | if (!offer->Initialize(desc, session_id_, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 358 | rtc::ToString(session_version_++))) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 359 | delete offer; |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 360 | PostCreateSessionDescriptionFailed(request.observer, |
| 361 | "Failed to initialize the offer."); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 362 | return; |
| 363 | } |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 364 | if (session_->local_description()) { |
| 365 | for (const cricket::ContentInfo& content : |
| 366 | session_->local_description()->description()->contents()) { |
| 367 | // Include all local ICE candidates in the SessionDescription unless |
| 368 | // the remote peer has requested an ICE restart. |
| 369 | if (!request.options.transport_options[content.name].ice_restart) { |
| 370 | CopyCandidatesFromSessionDescription(session_->local_description(), |
| 371 | content.name, offer); |
| 372 | } |
| 373 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 374 | } |
| 375 | PostCreateSessionDescriptionSucceeded(request.observer, offer); |
| 376 | } |
| 377 | |
| 378 | void WebRtcSessionDescriptionFactory::InternalCreateAnswer( |
| 379 | CreateSessionDescriptionRequest request) { |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 380 | if (session_->remote_description()) { |
| 381 | for (const cricket::ContentInfo& content : |
| 382 | session_->remote_description()->description()->contents()) { |
| 383 | // According to http://tools.ietf.org/html/rfc5245#section-9.2.1.1 |
| 384 | // an answer should also contain new ICE ufrag and password if an offer |
| 385 | // has been received with new ufrag and password. |
| 386 | request.options.transport_options[content.name].ice_restart = |
| 387 | session_->IceRestartPending(content.name); |
| 388 | // We should pass the current SSL role to the transport description |
| 389 | // factory, if there is already an existing ongoing session. |
| 390 | rtc::SSLRole ssl_role; |
| 391 | if (session_->GetSslRole(session_->GetChannel(content.name), &ssl_role)) { |
| 392 | request.options.transport_options[content.name].prefer_passive_role = |
| 393 | (rtc::SSL_SERVER == ssl_role); |
| 394 | } |
| 395 | } |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 396 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 397 | |
| 398 | cricket::SessionDescription* desc(session_desc_factory_.CreateAnswer( |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 399 | session_->remote_description() |
| 400 | ? session_->remote_description()->description() |
| 401 | : nullptr, |
| 402 | request.options, session_->local_description() |
| 403 | ? session_->local_description()->description() |
| 404 | : nullptr)); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 405 | // RFC 3264 |
| 406 | // If the answer is different from the offer in any way (different IP |
| 407 | // addresses, ports, etc.), the origin line MUST be different in the answer. |
| 408 | // In that case, the version number in the "o=" line of the answer is |
| 409 | // unrelated to the version number in the o line of the offer. |
| 410 | // Get a new version number by increasing the |session_version_answer_|. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 411 | // The |session_version_| is a uint64_t, the wrap around should not happen. |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 412 | ASSERT(session_version_ + 1 > session_version_); |
| 413 | JsepSessionDescription* answer(new JsepSessionDescription( |
| 414 | JsepSessionDescription::kAnswer)); |
| 415 | if (!answer->Initialize(desc, session_id_, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 416 | rtc::ToString(session_version_++))) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 417 | delete answer; |
| 418 | PostCreateSessionDescriptionFailed(request.observer, |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 419 | "Failed to initialize the answer."); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 420 | return; |
| 421 | } |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 422 | if (session_->local_description()) { |
| 423 | for (const cricket::ContentInfo& content : |
| 424 | session_->local_description()->description()->contents()) { |
| 425 | // Include all local ICE candidates in the SessionDescription unless |
| 426 | // the remote peer has requested an ICE restart. |
| 427 | if (!request.options.transport_options[content.name].ice_restart) { |
| 428 | CopyCandidatesFromSessionDescription(session_->local_description(), |
| 429 | content.name, answer); |
| 430 | } |
| 431 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 432 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 433 | PostCreateSessionDescriptionSucceeded(request.observer, answer); |
| 434 | } |
| 435 | |
tommi | 0f620f4 | 2015-07-09 03:25:02 -0700 | [diff] [blame] | 436 | void WebRtcSessionDescriptionFactory::FailPendingRequests( |
| 437 | const std::string& reason) { |
| 438 | ASSERT(signaling_thread_->IsCurrent()); |
| 439 | while (!create_session_description_requests_.empty()) { |
| 440 | const CreateSessionDescriptionRequest& request = |
| 441 | create_session_description_requests_.front(); |
| 442 | PostCreateSessionDescriptionFailed(request.observer, |
| 443 | ((request.type == CreateSessionDescriptionRequest::kOffer) ? |
| 444 | "CreateOffer" : "CreateAnswer") + reason); |
| 445 | create_session_description_requests_.pop(); |
| 446 | } |
| 447 | } |
| 448 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 449 | void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionFailed( |
| 450 | CreateSessionDescriptionObserver* observer, const std::string& error) { |
| 451 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 452 | msg->error = error; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame^] | 453 | signaling_thread_->Post(RTC_FROM_HERE, this, |
| 454 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 455 | LOG(LS_ERROR) << "Create SDP failed: " << error; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionSucceeded( |
| 459 | CreateSessionDescriptionObserver* observer, |
| 460 | SessionDescriptionInterface* description) { |
| 461 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 462 | msg->description.reset(description); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame^] | 463 | signaling_thread_->Post(RTC_FROM_HERE, this, |
| 464 | MSG_CREATE_SESSIONDESCRIPTION_SUCCESS, msg); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 467 | void WebRtcSessionDescriptionFactory::OnCertificateRequestFailed() { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 468 | ASSERT(signaling_thread_->IsCurrent()); |
| 469 | |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 470 | LOG(LS_ERROR) << "Asynchronous certificate generation request failed."; |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 471 | certificate_request_state_ = CERTIFICATE_FAILED; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 472 | |
tommi | 0f620f4 | 2015-07-09 03:25:02 -0700 | [diff] [blame] | 473 | FailPendingRequests(kFailedDueToIdentityFailed); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Henrik Boström | d828198 | 2015-08-27 10:12:24 +0200 | [diff] [blame] | 476 | void WebRtcSessionDescriptionFactory::SetCertificate( |
| 477 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 478 | RTC_DCHECK(certificate); |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 479 | LOG(LS_VERBOSE) << "Setting new certificate."; |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 480 | |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 481 | certificate_request_state_ = CERTIFICATE_SUCCEEDED; |
Henrik Boström | d828198 | 2015-08-27 10:12:24 +0200 | [diff] [blame] | 482 | SignalCertificateReady(certificate); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 483 | |
Henrik Boström | 3a14bf3 | 2015-08-31 09:27:58 +0200 | [diff] [blame] | 484 | transport_desc_factory_.set_certificate(certificate); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 485 | transport_desc_factory_.set_secure(cricket::SEC_ENABLED); |
| 486 | |
| 487 | while (!create_session_description_requests_.empty()) { |
| 488 | if (create_session_description_requests_.front().type == |
| 489 | CreateSessionDescriptionRequest::kOffer) { |
| 490 | InternalCreateOffer(create_session_description_requests_.front()); |
| 491 | } else { |
| 492 | InternalCreateAnswer(create_session_description_requests_.front()); |
| 493 | } |
| 494 | create_session_description_requests_.pop(); |
| 495 | } |
| 496 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 497 | } // namespace webrtc |