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