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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "pc/webrtcsessiondescriptionfactory.h" |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 12 | |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | #include <string> |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 15 | #include <utility> |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 16 | #include <vector> |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "api/jsep.h" |
| 19 | #include "api/jsepsessiondescription.h" |
| 20 | #include "api/mediaconstraintsinterface.h" |
Steve Anton | d5585ca | 2017-10-23 14:49:26 -0700 | [diff] [blame] | 21 | #include "pc/peerconnection.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/checks.h" |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 23 | #include "rtc_base/ptr_util.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "rtc_base/sslidentity.h" |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 25 | |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 26 | using cricket::MediaSessionOptions; |
| 27 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 28 | namespace webrtc { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 29 | namespace { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 30 | static const char kFailedDueToIdentityFailed[] = |
| 31 | " failed because DTLS identity request failed"; |
tommi | 0f620f4 | 2015-07-09 03:25:02 -0700 | [diff] [blame] | 32 | static const char kFailedDueToSessionShutdown[] = |
| 33 | " failed because the session was shut down"; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 34 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 35 | static const uint64_t kInitSessionVersion = 2; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 36 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 37 | static bool CompareSenderOptions(const cricket::SenderOptions& sender1, |
| 38 | const cricket::SenderOptions& sender2) { |
| 39 | return sender1.track_id < sender2.track_id; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 40 | } |
| 41 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 42 | static bool SameId(const cricket::SenderOptions& sender1, |
| 43 | const cricket::SenderOptions& sender2) { |
| 44 | return sender1.track_id == sender2.track_id; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 45 | } |
| 46 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 47 | // Check that each sender has a unique ID. |
| 48 | static 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.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | enum { |
| 64 | MSG_CREATE_SESSIONDESCRIPTION_SUCCESS, |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 65 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, |
| 66 | MSG_USE_CONSTRUCTOR_CERTIFICATE |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 69 | struct CreateSessionDescriptionMsg : public rtc::MessageData { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 70 | explicit CreateSessionDescriptionMsg( |
| 71 | webrtc::CreateSessionDescriptionObserver* observer) |
| 72 | : observer(observer) { |
| 73 | } |
| 74 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 75 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 76 | std::string error; |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 77 | std::unique_ptr<webrtc::SessionDescriptionInterface> description; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 78 | }; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 79 | } // namespace |
| 80 | |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 81 | void WebRtcCertificateGeneratorCallback::OnFailure() { |
| 82 | SignalRequestFailed(); |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 85 | void WebRtcCertificateGeneratorCallback::OnSuccess( |
| 86 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
| 87 | SignalCertificateReady(certificate); |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 88 | } |
| 89 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 90 | // static |
| 91 | void WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
| 92 | const SessionDescriptionInterface* source_desc, |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 93 | const std::string& content_name, |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 94 | SessionDescriptionInterface* dest_desc) { |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 95 | if (!source_desc) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 96 | return; |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 97 | } |
| 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 Brandstetter | 4eb1ddd | 2016-03-01 16:21:07 -0800 | [diff] [blame] | 110 | if (!source_candidates || !dest_candidates) { |
| 111 | return; |
| 112 | } |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 113 | 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.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 121 | // Private constructor called by other constructors. |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 122 | WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 123 | rtc::Thread* signaling_thread, |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 124 | cricket::ChannelManager* channel_manager, |
Steve Anton | d5585ca | 2017-10-23 14:49:26 -0700 | [diff] [blame] | 125 | PeerConnection* pc, |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 126 | const std::string& session_id, |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 127 | std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator, |
| 128 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 129 | : signaling_thread_(signaling_thread), |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 130 | 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öm | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 136 | cert_generator_(std::move(cert_generator)), |
Steve Anton | d5585ca | 2017-10-23 14:49:26 -0700 | [diff] [blame] | 137 | pc_(pc), |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 138 | session_id_(session_id), |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 139 | certificate_request_state_(CERTIFICATE_NOT_NEEDED) { |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 140 | RTC_DCHECK(signaling_thread_); |
Steve Anton | d5585ca | 2017-10-23 14:49:26 -0700 | [diff] [blame] | 141 | RTC_DCHECK(!(cert_generator_ && certificate)); |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 142 | bool dtls_enabled = cert_generator_ || certificate; |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 143 | // SRTP-SDES is disabled if DTLS is on. |
| 144 | SetSdesPolicy(dtls_enabled ? cricket::SEC_DISABLED : cricket::SEC_REQUIRED); |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 145 | if (!dtls_enabled) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 146 | RTC_LOG(LS_VERBOSE) << "DTLS-SRTP disabled."; |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 147 | return; |
| 148 | } |
| 149 | |
| 150 | if (certificate) { |
| 151 | // Use |certificate|. |
| 152 | certificate_request_state_ = CERTIFICATE_WAITING; |
| 153 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 154 | RTC_LOG(LS_VERBOSE) << "DTLS-SRTP enabled; has certificate parameter."; |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 155 | // 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 Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 159 | RTC_FROM_HERE, this, MSG_USE_CONSTRUCTOR_CERTIFICATE, |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 160 | 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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 173 | RTC_LOG(LS_VERBOSE) |
| 174 | << "DTLS-SRTP enabled; sending DTLS identity request (key " |
| 175 | << "type: " << key_params.type() << ")."; |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 176 | |
| 177 | // Request certificate. This happens asynchronously, so that the caller gets |
| 178 | // a chance to connect to |SignalCertificateReady|. |
Oskar Sundbom | 36f8f3e | 2017-11-16 10:54:27 +0100 | [diff] [blame] | 179 | cert_generator_->GenerateCertificateAsync(key_params, rtc::nullopt, |
| 180 | callback); |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 181 | } |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 182 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 183 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 184 | WebRtcSessionDescriptionFactory::~WebRtcSessionDescriptionFactory() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 185 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
tommi | 0f620f4 | 2015-07-09 03:25:02 -0700 | [diff] [blame] | 186 | |
| 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öm | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 194 | 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.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void WebRtcSessionDescriptionFactory::CreateOffer( |
| 210 | CreateSessionDescriptionObserver* observer, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 211 | const PeerConnectionInterface::RTCOfferAnswerOptions& options, |
| 212 | const cricket::MediaSessionOptions& session_options) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 213 | std::string error = "CreateOffer"; |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 214 | if (certificate_request_state_ == CERTIFICATE_FAILED) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 215 | error += kFailedDueToIdentityFailed; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 216 | RTC_LOG(LS_ERROR) << error; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 217 | PostCreateSessionDescriptionFailed(observer, error); |
| 218 | return; |
| 219 | } |
| 220 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 221 | if (!ValidMediaSessionOptions(session_options)) { |
| 222 | error += " called with invalid session options"; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 223 | RTC_LOG(LS_ERROR) << error; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 224 | PostCreateSessionDescriptionFailed(observer, error); |
| 225 | return; |
| 226 | } |
| 227 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 228 | CreateSessionDescriptionRequest request( |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 229 | CreateSessionDescriptionRequest::kOffer, observer, session_options); |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 230 | if (certificate_request_state_ == CERTIFICATE_WAITING) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 231 | create_session_description_requests_.push(request); |
| 232 | } else { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 233 | RTC_DCHECK(certificate_request_state_ == CERTIFICATE_SUCCEEDED || |
| 234 | certificate_request_state_ == CERTIFICATE_NOT_NEEDED); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 235 | InternalCreateOffer(request); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | void WebRtcSessionDescriptionFactory::CreateAnswer( |
| 240 | CreateSessionDescriptionObserver* observer, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 241 | const cricket::MediaSessionOptions& session_options) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 242 | std::string error = "CreateAnswer"; |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 243 | if (certificate_request_state_ == CERTIFICATE_FAILED) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 244 | error += kFailedDueToIdentityFailed; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 245 | RTC_LOG(LS_ERROR) << error; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 246 | PostCreateSessionDescriptionFailed(observer, error); |
| 247 | return; |
| 248 | } |
Steve Anton | d5585ca | 2017-10-23 14:49:26 -0700 | [diff] [blame] | 249 | if (!pc_->remote_description()) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 250 | error += " can't be called before SetRemoteDescription."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 251 | RTC_LOG(LS_ERROR) << error; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 252 | PostCreateSessionDescriptionFailed(observer, error); |
| 253 | return; |
| 254 | } |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 255 | if (pc_->remote_description()->GetType() != SdpType::kOffer) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 256 | error += " failed because remote_description is not an offer."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 257 | RTC_LOG(LS_ERROR) << error; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 258 | PostCreateSessionDescriptionFailed(observer, error); |
| 259 | return; |
| 260 | } |
| 261 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 262 | if (!ValidMediaSessionOptions(session_options)) { |
| 263 | error += " called with invalid session options."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 264 | RTC_LOG(LS_ERROR) << error; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 265 | PostCreateSessionDescriptionFailed(observer, error); |
| 266 | return; |
| 267 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 268 | |
| 269 | CreateSessionDescriptionRequest request( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 270 | CreateSessionDescriptionRequest::kAnswer, observer, session_options); |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 271 | if (certificate_request_state_ == CERTIFICATE_WAITING) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 272 | create_session_description_requests_.push(request); |
| 273 | } else { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 274 | RTC_DCHECK(certificate_request_state_ == CERTIFICATE_SUCCEEDED || |
| 275 | certificate_request_state_ == CERTIFICATE_NOT_NEEDED); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 276 | InternalCreateAnswer(request); |
| 277 | } |
| 278 | } |
| 279 | |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 280 | void WebRtcSessionDescriptionFactory::SetSdesPolicy( |
| 281 | cricket::SecurePolicy secure_policy) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 282 | session_desc_factory_.set_secure(secure_policy); |
| 283 | } |
| 284 | |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 285 | cricket::SecurePolicy WebRtcSessionDescriptionFactory::SdesPolicy() const { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 286 | return session_desc_factory_.secure(); |
| 287 | } |
| 288 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 289 | void WebRtcSessionDescriptionFactory::OnMessage(rtc::Message* msg) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 290 | 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öm | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 305 | case MSG_USE_CONSTRUCTOR_CERTIFICATE: { |
| 306 | rtc::ScopedRefMessageData<rtc::RTCCertificate>* param = |
| 307 | static_cast<rtc::ScopedRefMessageData<rtc::RTCCertificate>*>( |
| 308 | msg->pdata); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 309 | RTC_LOG(LS_INFO) << "Using certificate supplied to the constructor."; |
Henrik Boström | d828198 | 2015-08-27 10:12:24 +0200 | [diff] [blame] | 310 | SetCertificate(param->data()); |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 311 | delete param; |
| 312 | break; |
| 313 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 314 | default: |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 315 | RTC_NOTREACHED(); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 316 | break; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | void WebRtcSessionDescriptionFactory::InternalCreateOffer( |
| 321 | CreateSessionDescriptionRequest request) { |
Steve Anton | d5585ca | 2017-10-23 14:49:26 -0700 | [diff] [blame] | 322 | if (pc_->local_description()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 323 | // 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 Anton | d5585ca | 2017-10-23 14:49:26 -0700 | [diff] [blame] | 327 | if (pc_->NeedsIceRestart(options.mid)) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 328 | options.transport_options.ice_restart = true; |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 333 | cricket::SessionDescription* desc(session_desc_factory_.CreateOffer( |
Steve Anton | d5585ca | 2017-10-23 14:49:26 -0700 | [diff] [blame] | 334 | request.options, pc_->local_description() |
| 335 | ? pc_->local_description()->description() |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 336 | : nullptr)); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 337 | // 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öm | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 345 | // The |session_version_| is a uint64_t, the wrap around should not happen. |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 346 | RTC_DCHECK(session_version_ + 1 > session_version_); |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 347 | auto offer = rtc::MakeUnique<JsepSessionDescription>(SdpType::kOffer); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 348 | if (!offer->Initialize(desc, session_id_, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 349 | rtc::ToString(session_version_++))) { |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 350 | PostCreateSessionDescriptionFailed(request.observer, |
| 351 | "Failed to initialize the offer."); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 352 | return; |
| 353 | } |
Steve Anton | d5585ca | 2017-10-23 14:49:26 -0700 | [diff] [blame] | 354 | if (pc_->local_description()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 355 | for (const cricket::MediaDescriptionOptions& options : |
| 356 | request.options.media_description_options) { |
| 357 | if (!options.transport_options.ice_restart) { |
Steve Anton | d5585ca | 2017-10-23 14:49:26 -0700 | [diff] [blame] | 358 | CopyCandidatesFromSessionDescription(pc_->local_description(), |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 359 | options.mid, offer.get()); |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 360 | } |
| 361 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 362 | } |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 363 | PostCreateSessionDescriptionSucceeded(request.observer, std::move(offer)); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | void WebRtcSessionDescriptionFactory::InternalCreateAnswer( |
| 367 | CreateSessionDescriptionRequest request) { |
Steve Anton | d5585ca | 2017-10-23 14:49:26 -0700 | [diff] [blame] | 368 | if (pc_->remote_description()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 369 | for (cricket::MediaDescriptionOptions& options : |
| 370 | request.options.media_description_options) { |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 371 | // 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. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 374 | options.transport_options.ice_restart = |
Steve Anton | d5585ca | 2017-10-23 14:49:26 -0700 | [diff] [blame] | 375 | pc_->IceRestartPending(options.mid); |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 376 | // 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 Anton | d5585ca | 2017-10-23 14:49:26 -0700 | [diff] [blame] | 379 | if (pc_->GetSslRole(options.mid, &ssl_role)) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 380 | options.transport_options.prefer_passive_role = |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 381 | (rtc::SSL_SERVER == ssl_role); |
| 382 | } |
| 383 | } |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 384 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 385 | |
| 386 | cricket::SessionDescription* desc(session_desc_factory_.CreateAnswer( |
Steve Anton | d5585ca | 2017-10-23 14:49:26 -0700 | [diff] [blame] | 387 | pc_->remote_description() ? pc_->remote_description()->description() |
| 388 | : nullptr, |
| 389 | request.options, |
| 390 | pc_->local_description() ? pc_->local_description()->description() |
| 391 | : nullptr)); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 392 | // 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öm | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 398 | // The |session_version_| is a uint64_t, the wrap around should not happen. |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 399 | RTC_DCHECK(session_version_ + 1 > session_version_); |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 400 | auto answer = rtc::MakeUnique<JsepSessionDescription>(SdpType::kAnswer); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 401 | if (!answer->Initialize(desc, session_id_, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 402 | rtc::ToString(session_version_++))) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 403 | PostCreateSessionDescriptionFailed(request.observer, |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 404 | "Failed to initialize the answer."); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 405 | return; |
| 406 | } |
Steve Anton | d5585ca | 2017-10-23 14:49:26 -0700 | [diff] [blame] | 407 | if (pc_->local_description()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 408 | // 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 Anton | d5585ca | 2017-10-23 14:49:26 -0700 | [diff] [blame] | 413 | CopyCandidatesFromSessionDescription(pc_->local_description(), |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 414 | options.mid, answer.get()); |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 415 | } |
| 416 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 417 | } |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 418 | PostCreateSessionDescriptionSucceeded(request.observer, std::move(answer)); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 419 | } |
| 420 | |
tommi | 0f620f4 | 2015-07-09 03:25:02 -0700 | [diff] [blame] | 421 | void WebRtcSessionDescriptionFactory::FailPendingRequests( |
| 422 | const std::string& reason) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 423 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
tommi | 0f620f4 | 2015-07-09 03:25:02 -0700 | [diff] [blame] | 424 | 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.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 434 | void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionFailed( |
| 435 | CreateSessionDescriptionObserver* observer, const std::string& error) { |
| 436 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 437 | msg->error = error; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 438 | signaling_thread_->Post(RTC_FROM_HERE, this, |
| 439 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 440 | RTC_LOG(LS_ERROR) << "Create SDP failed: " << error; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionSucceeded( |
| 444 | CreateSessionDescriptionObserver* observer, |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 445 | std::unique_ptr<SessionDescriptionInterface> description) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 446 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 447 | msg->description = std::move(description); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 448 | signaling_thread_->Post(RTC_FROM_HERE, this, |
| 449 | MSG_CREATE_SESSIONDESCRIPTION_SUCCESS, msg); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 452 | void WebRtcSessionDescriptionFactory::OnCertificateRequestFailed() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 453 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 454 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 455 | RTC_LOG(LS_ERROR) << "Asynchronous certificate generation request failed."; |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 456 | certificate_request_state_ = CERTIFICATE_FAILED; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 457 | |
tommi | 0f620f4 | 2015-07-09 03:25:02 -0700 | [diff] [blame] | 458 | FailPendingRequests(kFailedDueToIdentityFailed); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Henrik Boström | d828198 | 2015-08-27 10:12:24 +0200 | [diff] [blame] | 461 | void WebRtcSessionDescriptionFactory::SetCertificate( |
| 462 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 463 | RTC_DCHECK(certificate); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 464 | RTC_LOG(LS_VERBOSE) << "Setting new certificate."; |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 465 | |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 466 | certificate_request_state_ = CERTIFICATE_SUCCEEDED; |
Henrik Boström | d828198 | 2015-08-27 10:12:24 +0200 | [diff] [blame] | 467 | SignalCertificateReady(certificate); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 468 | |
Henrik Boström | 3a14bf3 | 2015-08-31 09:27:58 +0200 | [diff] [blame] | 469 | transport_desc_factory_.set_certificate(certificate); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 470 | 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.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 482 | } // namespace webrtc |