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