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