wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 2 | * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 15:54:43 | [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 | [diff] [blame] | 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #include "pc/webrtc_session_description_factory.h" |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 12 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 13 | #include <stddef.h> |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 14 | |
Harald Alvestrand | d153de6 | 2024-09-16 20:28:14 | [diff] [blame] | 15 | #include <cstdint> |
| 16 | #include <functional> |
| 17 | #include <memory> |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 18 | #include <optional> |
Danil Chapovalov | 97bdfa3 | 2022-08-22 17:45:43 | [diff] [blame] | 19 | #include <queue> |
Steve Anton | 36b29d1 | 2017-10-30 16:57:42 | [diff] [blame] | 20 | #include <string> |
kwiberg | 0eb15ed | 2015-12-17 11:04:15 | [diff] [blame] | 21 | #include <utility> |
Steve Anton | 36b29d1 | 2017-10-30 16:57:42 | [diff] [blame] | 22 | #include <vector> |
kwiberg | 0eb15ed | 2015-12-17 11:04:15 | [diff] [blame] | 23 | |
Steve Anton | 64b626b | 2019-01-29 01:25:26 | [diff] [blame] | 24 | #include "absl/algorithm/container.h" |
Harald Alvestrand | d153de6 | 2024-09-16 20:28:14 | [diff] [blame] | 25 | #include "absl/functional/any_invocable.h" |
| 26 | #include "api/field_trials_view.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 27 | #include "api/jsep.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 28 | #include "api/jsep_session_description.h" |
Harald Alvestrand | d153de6 | 2024-09-16 20:28:14 | [diff] [blame] | 29 | #include "api/peer_connection_interface.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 30 | #include "api/rtc_error.h" |
Harald Alvestrand | d153de6 | 2024-09-16 20:28:14 | [diff] [blame] | 31 | #include "api/scoped_refptr.h" |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 32 | #include "api/sequence_checker.h" |
Harald Alvestrand | d153de6 | 2024-09-16 20:28:14 | [diff] [blame] | 33 | #include "call/payload_type.h" |
Jonas Oreland | ed99dae | 2022-03-09 08:28:10 | [diff] [blame] | 34 | #include "pc/connection_context.h" |
Harald Alvestrand | d153de6 | 2024-09-16 20:28:14 | [diff] [blame] | 35 | #include "pc/media_session.h" |
Harald Alvestrand | f01bd6c | 2020-10-23 13:30:46 | [diff] [blame] | 36 | #include "pc/sdp_state_provider.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 37 | #include "pc/session_description.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 38 | #include "rtc_base/checks.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 39 | #include "rtc_base/logging.h" |
Harald Alvestrand | d153de6 | 2024-09-16 20:28:14 | [diff] [blame] | 40 | #include "rtc_base/rtc_certificate.h" |
| 41 | #include "rtc_base/rtc_certificate_generator.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 42 | #include "rtc_base/ssl_identity.h" |
| 43 | #include "rtc_base/ssl_stream_adapter.h" |
| 44 | #include "rtc_base/string_encode.h" |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 45 | #include "rtc_base/unique_id_generator.h" |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 46 | |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 47 | using cricket::MediaSessionOptions; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 48 | using rtc::UniqueRandomIdGenerator; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 49 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 50 | namespace webrtc { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 51 | namespace { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 52 | static const char kFailedDueToIdentityFailed[] = |
| 53 | " failed because DTLS identity request failed"; |
tommi | 0f620f4 | 2015-07-09 10:25:02 | [diff] [blame] | 54 | static const char kFailedDueToSessionShutdown[] = |
| 55 | " failed because the session was shut down"; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 56 | |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 57 | static const uint64_t kInitSessionVersion = 2; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 58 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 59 | // Check that each sender has a unique ID. |
| 60 | static bool ValidMediaSessionOptions( |
| 61 | const cricket::MediaSessionOptions& session_options) { |
| 62 | std::vector<cricket::SenderOptions> sorted_senders; |
| 63 | for (const cricket::MediaDescriptionOptions& media_description_options : |
| 64 | session_options.media_description_options) { |
| 65 | sorted_senders.insert(sorted_senders.end(), |
| 66 | media_description_options.sender_options.begin(), |
| 67 | media_description_options.sender_options.end()); |
| 68 | } |
Steve Anton | 64b626b | 2019-01-29 01:25:26 | [diff] [blame] | 69 | absl::c_sort(sorted_senders, [](const cricket::SenderOptions& sender1, |
| 70 | const cricket::SenderOptions& sender2) { |
| 71 | return sender1.track_id < sender2.track_id; |
| 72 | }); |
| 73 | return absl::c_adjacent_find(sorted_senders, |
| 74 | [](const cricket::SenderOptions& sender1, |
| 75 | const cricket::SenderOptions& sender2) { |
| 76 | return sender1.track_id == sender2.track_id; |
| 77 | }) == sorted_senders.end(); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 78 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 79 | } // namespace |
| 80 | |
| 81 | // static |
| 82 | void WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
| 83 | const SessionDescriptionInterface* source_desc, |
deadbeef | 0ed85b2 | 2016-02-24 01:24:52 | [diff] [blame] | 84 | const std::string& content_name, |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 85 | SessionDescriptionInterface* dest_desc) { |
deadbeef | 0ed85b2 | 2016-02-24 01:24:52 | [diff] [blame] | 86 | if (!source_desc) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 87 | return; |
deadbeef | 0ed85b2 | 2016-02-24 01:24:52 | [diff] [blame] | 88 | } |
| 89 | const cricket::ContentInfos& contents = |
| 90 | source_desc->description()->contents(); |
| 91 | const cricket::ContentInfo* cinfo = |
| 92 | source_desc->description()->GetContentByName(content_name); |
| 93 | if (!cinfo) { |
| 94 | return; |
| 95 | } |
| 96 | size_t mediasection_index = static_cast<int>(cinfo - &contents[0]); |
| 97 | const IceCandidateCollection* source_candidates = |
| 98 | source_desc->candidates(mediasection_index); |
| 99 | const IceCandidateCollection* dest_candidates = |
| 100 | dest_desc->candidates(mediasection_index); |
Taylor Brandstetter | 4eb1ddd | 2016-03-02 00:21:07 | [diff] [blame] | 101 | if (!source_candidates || !dest_candidates) { |
| 102 | return; |
| 103 | } |
deadbeef | 0ed85b2 | 2016-02-24 01:24:52 | [diff] [blame] | 104 | for (size_t n = 0; n < source_candidates->count(); ++n) { |
| 105 | const IceCandidateInterface* new_candidate = source_candidates->at(n); |
| 106 | if (!dest_candidates->HasCandidate(new_candidate)) { |
| 107 | dest_desc->AddCandidate(source_candidates->at(n)); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory( |
Jonas Oreland | ed99dae | 2022-03-09 08:28:10 | [diff] [blame] | 113 | ConnectionContext* context, |
Harald Alvestrand | f01bd6c | 2020-10-23 13:30:46 | [diff] [blame] | 114 | const SdpStateProvider* sdp_info, |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 115 | const std::string& session_id, |
Harald Alvestrand | d89ce53 | 2020-10-21 08:59:22 | [diff] [blame] | 116 | bool dtls_enabled, |
Henrik Boström | d03c23b | 2016-06-01 09:44:18 | [diff] [blame] | 117 | std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator, |
Danil Chapovalov | b7da816 | 2022-08-22 14:39:34 | [diff] [blame] | 118 | rtc::scoped_refptr<rtc::RTCCertificate> certificate, |
Harald Alvestrand | a094787 | 2020-11-09 14:15:00 | [diff] [blame] | 119 | std::function<void(const rtc::scoped_refptr<rtc::RTCCertificate>&)> |
Jonas Oreland | 6c7f984 | 2022-04-19 15:24:10 | [diff] [blame] | 120 | on_certificate_ready, |
Harald Alvestrand | d153de6 | 2024-09-16 20:28:14 | [diff] [blame] | 121 | PayloadTypeSuggester* pt_suggester, |
Jonas Oreland | 6c7f984 | 2022-04-19 15:24:10 | [diff] [blame] | 122 | const FieldTrialsView& field_trials) |
Jonas Oreland | ed99dae | 2022-03-09 08:28:10 | [diff] [blame] | 123 | : signaling_thread_(context->signaling_thread()), |
Jonas Oreland | 6c7f984 | 2022-04-19 15:24:10 | [diff] [blame] | 124 | transport_desc_factory_(field_trials), |
Harald Alvestrand | c3fa7c3 | 2022-05-22 10:57:01 | [diff] [blame] | 125 | session_desc_factory_(context->media_engine(), |
| 126 | context->use_rtx(), |
| 127 | context->ssrc_generator(), |
Harald Alvestrand | d153de6 | 2024-09-16 20:28:14 | [diff] [blame] | 128 | &transport_desc_factory_, |
| 129 | pt_suggester), |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 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 |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 133 | // `kInitSessionVersion`. |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 134 | session_version_(kInitSessionVersion), |
Harald Alvestrand | d89ce53 | 2020-10-21 08:59:22 | [diff] [blame] | 135 | cert_generator_(dtls_enabled ? std::move(cert_generator) : nullptr), |
Harald Alvestrand | f01bd6c | 2020-10-23 13:30:46 | [diff] [blame] | 136 | sdp_info_(sdp_info), |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 137 | session_id_(session_id), |
Harald Alvestrand | a094787 | 2020-11-09 14:15:00 | [diff] [blame] | 138 | certificate_request_state_(CERTIFICATE_NOT_NEEDED), |
| 139 | on_certificate_ready_(on_certificate_ready) { |
Henrik Boström | d03c23b | 2016-06-01 09:44:18 | [diff] [blame] | 140 | RTC_DCHECK(signaling_thread_); |
Harald Alvestrand | d89ce53 | 2020-10-21 08:59:22 | [diff] [blame] | 141 | |
Henrik Boström | d03c23b | 2016-06-01 09:44:18 | [diff] [blame] | 142 | if (!dtls_enabled) { |
Harald Alvestrand | 974044e | 2024-02-08 13:15:51 | [diff] [blame] | 143 | RTC_LOG(LS_INFO) << "DTLS-SRTP disabled"; |
| 144 | transport_desc_factory_.SetInsecureForTesting(); |
Henrik Boström | d03c23b | 2016-06-01 09:44:18 | [diff] [blame] | 145 | return; |
| 146 | } |
Henrik Boström | d03c23b | 2016-06-01 09:44:18 | [diff] [blame] | 147 | if (certificate) { |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 148 | // Use `certificate`. |
Henrik Boström | d03c23b | 2016-06-01 09:44:18 | [diff] [blame] | 149 | certificate_request_state_ = CERTIFICATE_WAITING; |
| 150 | |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 151 | RTC_LOG(LS_VERBOSE) << "DTLS-SRTP enabled; has certificate parameter."; |
Danil Chapovalov | b22f0c2 | 2022-08-19 12:03:54 | [diff] [blame] | 152 | RTC_LOG(LS_INFO) << "Using certificate supplied to the constructor."; |
| 153 | SetCertificate(certificate); |
Harald Alvestrand | 974044e | 2024-02-08 13:15:51 | [diff] [blame] | 154 | return; |
Henrik Boström | d03c23b | 2016-06-01 09:44:18 | [diff] [blame] | 155 | } |
Harald Alvestrand | 974044e | 2024-02-08 13:15:51 | [diff] [blame] | 156 | // Generate certificate. |
| 157 | RTC_DCHECK(cert_generator_); |
| 158 | certificate_request_state_ = CERTIFICATE_WAITING; |
| 159 | |
| 160 | auto callback = [weak_ptr = weak_factory_.GetWeakPtr()]( |
| 161 | rtc::scoped_refptr<rtc::RTCCertificate> certificate) { |
| 162 | if (!weak_ptr) { |
| 163 | return; |
| 164 | } |
| 165 | if (certificate) { |
| 166 | weak_ptr->SetCertificate(std::move(certificate)); |
| 167 | } else { |
| 168 | weak_ptr->OnCertificateRequestFailed(); |
| 169 | } |
| 170 | }; |
| 171 | |
| 172 | rtc::KeyParams key_params = rtc::KeyParams(); |
| 173 | RTC_LOG(LS_VERBOSE) |
| 174 | << "DTLS-SRTP enabled; sending DTLS identity request (key type: " |
| 175 | << key_params.type() << ")."; |
| 176 | |
| 177 | // Request certificate. This happens asynchronously on a different thread. |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 178 | cert_generator_->GenerateCertificateAsync(key_params, std::nullopt, |
Harald Alvestrand | 974044e | 2024-02-08 13:15:51 | [diff] [blame] | 179 | std::move(callback)); |
Henrik Boström | 87713d0 | 2015-08-25 07:53:21 | [diff] [blame] | 180 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 181 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 182 | WebRtcSessionDescriptionFactory::~WebRtcSessionDescriptionFactory() { |
Tomas Gunnarsson | 20f7456 | 2021-02-04 09:22:50 | [diff] [blame] | 183 | RTC_DCHECK_RUN_ON(signaling_thread_); |
tommi | 0f620f4 | 2015-07-09 10:25:02 | [diff] [blame] | 184 | |
| 185 | // Fail any requests that were asked for before identity generation completed. |
| 186 | FailPendingRequests(kFailedDueToSessionShutdown); |
| 187 | |
Danil Chapovalov | 97bdfa3 | 2022-08-22 17:45:43 | [diff] [blame] | 188 | // Process all pending notifications. If we don't do this, requests will |
| 189 | // linger and not know they succeeded or failed. |
| 190 | // All tasks that suppose to run them are protected with weak_factory_ and |
| 191 | // will be cancelled. If we don't protect them, they might trigger after peer |
| 192 | // connection is destroyed, which might be surprising. |
| 193 | while (!callbacks_.empty()) { |
| 194 | std::move(callbacks_.front())(); |
| 195 | callbacks_.pop(); |
Henrik Boström | 87713d0 | 2015-08-25 07:53:21 | [diff] [blame] | 196 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void WebRtcSessionDescriptionFactory::CreateOffer( |
| 200 | CreateSessionDescriptionObserver* observer, |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 201 | const PeerConnectionInterface::RTCOfferAnswerOptions& options, |
| 202 | const cricket::MediaSessionOptions& session_options) { |
Tomas Gunnarsson | 20f7456 | 2021-02-04 09:22:50 | [diff] [blame] | 203 | RTC_DCHECK_RUN_ON(signaling_thread_); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 204 | std::string error = "CreateOffer"; |
Henrik Boström | 87713d0 | 2015-08-25 07:53:21 | [diff] [blame] | 205 | if (certificate_request_state_ == CERTIFICATE_FAILED) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 206 | error += kFailedDueToIdentityFailed; |
Philipp Hancke | b916a70 | 2023-09-18 11:06:24 | [diff] [blame] | 207 | PostCreateSessionDescriptionFailed( |
| 208 | observer, RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error))); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 209 | return; |
| 210 | } |
| 211 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 212 | if (!ValidMediaSessionOptions(session_options)) { |
| 213 | error += " called with invalid session options"; |
Philipp Hancke | b916a70 | 2023-09-18 11:06:24 | [diff] [blame] | 214 | PostCreateSessionDescriptionFailed( |
| 215 | observer, RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error))); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 216 | return; |
| 217 | } |
| 218 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 219 | CreateSessionDescriptionRequest request( |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 | [diff] [blame] | 220 | CreateSessionDescriptionRequest::kOffer, observer, session_options); |
Henrik Boström | 87713d0 | 2015-08-25 07:53:21 | [diff] [blame] | 221 | if (certificate_request_state_ == CERTIFICATE_WAITING) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 222 | create_session_description_requests_.push(request); |
| 223 | } else { |
nisse | ede5da4 | 2017-01-12 13:15:36 | [diff] [blame] | 224 | RTC_DCHECK(certificate_request_state_ == CERTIFICATE_SUCCEEDED || |
| 225 | certificate_request_state_ == CERTIFICATE_NOT_NEEDED); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 226 | InternalCreateOffer(request); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | void WebRtcSessionDescriptionFactory::CreateAnswer( |
| 231 | CreateSessionDescriptionObserver* observer, |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 232 | const cricket::MediaSessionOptions& session_options) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 233 | std::string error = "CreateAnswer"; |
Henrik Boström | 87713d0 | 2015-08-25 07:53:21 | [diff] [blame] | 234 | if (certificate_request_state_ == CERTIFICATE_FAILED) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 235 | error += kFailedDueToIdentityFailed; |
Philipp Hancke | b916a70 | 2023-09-18 11:06:24 | [diff] [blame] | 236 | PostCreateSessionDescriptionFailed( |
| 237 | observer, RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error))); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 238 | return; |
| 239 | } |
Harald Alvestrand | f01bd6c | 2020-10-23 13:30:46 | [diff] [blame] | 240 | if (!sdp_info_->remote_description()) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 241 | error += " can't be called before SetRemoteDescription."; |
Philipp Hancke | b916a70 | 2023-09-18 11:06:24 | [diff] [blame] | 242 | PostCreateSessionDescriptionFailed( |
| 243 | observer, RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error))); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 244 | return; |
| 245 | } |
Harald Alvestrand | f01bd6c | 2020-10-23 13:30:46 | [diff] [blame] | 246 | if (sdp_info_->remote_description()->GetType() != SdpType::kOffer) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 247 | error += " failed because remote_description is not an offer."; |
Philipp Hancke | b916a70 | 2023-09-18 11:06:24 | [diff] [blame] | 248 | PostCreateSessionDescriptionFailed( |
| 249 | observer, RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error))); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 250 | return; |
| 251 | } |
| 252 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 253 | if (!ValidMediaSessionOptions(session_options)) { |
| 254 | error += " called with invalid session options."; |
Philipp Hancke | b916a70 | 2023-09-18 11:06:24 | [diff] [blame] | 255 | PostCreateSessionDescriptionFailed( |
| 256 | observer, RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error))); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 257 | return; |
| 258 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 259 | |
| 260 | CreateSessionDescriptionRequest request( |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 261 | CreateSessionDescriptionRequest::kAnswer, observer, session_options); |
Henrik Boström | 87713d0 | 2015-08-25 07:53:21 | [diff] [blame] | 262 | if (certificate_request_state_ == CERTIFICATE_WAITING) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 263 | create_session_description_requests_.push(request); |
| 264 | } else { |
nisse | ede5da4 | 2017-01-12 13:15:36 | [diff] [blame] | 265 | RTC_DCHECK(certificate_request_state_ == CERTIFICATE_SUCCEEDED || |
| 266 | certificate_request_state_ == CERTIFICATE_NOT_NEEDED); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 267 | InternalCreateAnswer(request); |
| 268 | } |
| 269 | } |
| 270 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 271 | void WebRtcSessionDescriptionFactory::InternalCreateOffer( |
| 272 | CreateSessionDescriptionRequest request) { |
Harald Alvestrand | f01bd6c | 2020-10-23 13:30:46 | [diff] [blame] | 273 | if (sdp_info_->local_description()) { |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 274 | // If the needs-ice-restart flag is set as described by JSEP, we should |
| 275 | // generate an offer with a new ufrag/password to trigger an ICE restart. |
| 276 | for (cricket::MediaDescriptionOptions& options : |
| 277 | request.options.media_description_options) { |
Harald Alvestrand | f01bd6c | 2020-10-23 13:30:46 | [diff] [blame] | 278 | if (sdp_info_->NeedsIceRestart(options.mid)) { |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 279 | options.transport_options.ice_restart = true; |
deadbeef | d1a38b5 | 2016-12-10 21:15:33 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
Philipp Hancke | 2bf1b99 | 2023-09-26 07:04:46 | [diff] [blame] | 284 | auto result = session_desc_factory_.CreateOfferOrError( |
| 285 | request.options, sdp_info_->local_description() |
| 286 | ? sdp_info_->local_description()->description() |
| 287 | : nullptr); |
| 288 | if (!result.ok()) { |
| 289 | PostCreateSessionDescriptionFailed(request.observer.get(), result.error()); |
Steve Anton | 6fe1fba | 2018-12-11 18:15:23 | [diff] [blame] | 290 | return; |
| 291 | } |
Philipp Hancke | 2bf1b99 | 2023-09-26 07:04:46 | [diff] [blame] | 292 | std::unique_ptr<cricket::SessionDescription> desc = std::move(result.value()); |
| 293 | RTC_CHECK(desc); |
Steve Anton | 6fe1fba | 2018-12-11 18:15:23 | [diff] [blame] | 294 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 295 | // RFC 3264 |
| 296 | // When issuing an offer that modifies the session, |
| 297 | // the "o=" line of the new SDP MUST be identical to that in the |
| 298 | // previous SDP, except that the version in the origin field MUST |
| 299 | // increment by one from the previous SDP. |
| 300 | |
| 301 | // Just increase the version number by one each time when a new offer |
| 302 | // is created regardless if it's identical to the previous one or not. |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 303 | // The `session_version_` is a uint64_t, the wrap around should not happen. |
nisse | ede5da4 | 2017-01-12 13:15:36 | [diff] [blame] | 304 | RTC_DCHECK(session_version_ + 1 > session_version_); |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 305 | auto offer = std::make_unique<JsepSessionDescription>( |
Steve Anton | 6fe1fba | 2018-12-11 18:15:23 | [diff] [blame] | 306 | SdpType::kOffer, std::move(desc), session_id_, |
| 307 | rtc::ToString(session_version_++)); |
Harald Alvestrand | f01bd6c | 2020-10-23 13:30:46 | [diff] [blame] | 308 | if (sdp_info_->local_description()) { |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 309 | for (const cricket::MediaDescriptionOptions& options : |
| 310 | request.options.media_description_options) { |
| 311 | if (!options.transport_options.ice_restart) { |
Harald Alvestrand | f01bd6c | 2020-10-23 13:30:46 | [diff] [blame] | 312 | CopyCandidatesFromSessionDescription(sdp_info_->local_description(), |
Steve Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 313 | options.mid, offer.get()); |
deadbeef | 0ed85b2 | 2016-02-24 01:24:52 | [diff] [blame] | 314 | } |
| 315 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 316 | } |
Niels Möller | afb246b | 2022-04-20 12:26:50 | [diff] [blame] | 317 | PostCreateSessionDescriptionSucceeded(request.observer.get(), |
| 318 | std::move(offer)); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | void WebRtcSessionDescriptionFactory::InternalCreateAnswer( |
| 322 | CreateSessionDescriptionRequest request) { |
Harald Alvestrand | f01bd6c | 2020-10-23 13:30:46 | [diff] [blame] | 323 | if (sdp_info_->remote_description()) { |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 324 | for (cricket::MediaDescriptionOptions& options : |
| 325 | request.options.media_description_options) { |
deadbeef | 0ed85b2 | 2016-02-24 01:24:52 | [diff] [blame] | 326 | // According to http://tools.ietf.org/html/rfc5245#section-9.2.1.1 |
| 327 | // an answer should also contain new ICE ufrag and password if an offer |
| 328 | // has been received with new ufrag and password. |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 329 | options.transport_options.ice_restart = |
Harald Alvestrand | f01bd6c | 2020-10-23 13:30:46 | [diff] [blame] | 330 | sdp_info_->IceRestartPending(options.mid); |
Harald Alvestrand | d89ce53 | 2020-10-21 08:59:22 | [diff] [blame] | 331 | // We should pass the current DTLS role to the transport description |
deadbeef | 0ed85b2 | 2016-02-24 01:24:52 | [diff] [blame] | 332 | // factory, if there is already an existing ongoing session. |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 333 | std::optional<rtc::SSLRole> dtls_role = |
Harald Alvestrand | f01bd6c | 2020-10-23 13:30:46 | [diff] [blame] | 334 | sdp_info_->GetDtlsRole(options.mid); |
Harald Alvestrand | d89ce53 | 2020-10-21 08:59:22 | [diff] [blame] | 335 | if (dtls_role) { |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 336 | options.transport_options.prefer_passive_role = |
Harald Alvestrand | d89ce53 | 2020-10-21 08:59:22 | [diff] [blame] | 337 | (rtc::SSL_SERVER == *dtls_role); |
deadbeef | 0ed85b2 | 2016-02-24 01:24:52 | [diff] [blame] | 338 | } |
| 339 | } |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 | [diff] [blame] | 340 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 341 | |
Philipp Hancke | 2bf1b99 | 2023-09-26 07:04:46 | [diff] [blame] | 342 | auto result = session_desc_factory_.CreateAnswerOrError( |
| 343 | sdp_info_->remote_description() |
| 344 | ? sdp_info_->remote_description()->description() |
| 345 | : nullptr, |
| 346 | request.options, |
| 347 | sdp_info_->local_description() |
| 348 | ? sdp_info_->local_description()->description() |
| 349 | : nullptr); |
| 350 | if (!result.ok()) { |
| 351 | PostCreateSessionDescriptionFailed(request.observer.get(), result.error()); |
Steve Anton | 6fe1fba | 2018-12-11 18:15:23 | [diff] [blame] | 352 | return; |
| 353 | } |
Philipp Hancke | 2bf1b99 | 2023-09-26 07:04:46 | [diff] [blame] | 354 | std::unique_ptr<cricket::SessionDescription> desc = std::move(result.value()); |
| 355 | RTC_CHECK(desc); |
Steve Anton | 6fe1fba | 2018-12-11 18:15:23 | [diff] [blame] | 356 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 357 | // RFC 3264 |
| 358 | // If the answer is different from the offer in any way (different IP |
| 359 | // addresses, ports, etc.), the origin line MUST be different in the answer. |
| 360 | // In that case, the version number in the "o=" line of the answer is |
| 361 | // unrelated to the version number in the o line of the offer. |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 362 | // Get a new version number by increasing the `session_version_answer_`. |
| 363 | // The `session_version_` is a uint64_t, the wrap around should not happen. |
nisse | ede5da4 | 2017-01-12 13:15:36 | [diff] [blame] | 364 | RTC_DCHECK(session_version_ + 1 > session_version_); |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 365 | auto answer = std::make_unique<JsepSessionDescription>( |
Steve Anton | 6fe1fba | 2018-12-11 18:15:23 | [diff] [blame] | 366 | SdpType::kAnswer, std::move(desc), session_id_, |
| 367 | rtc::ToString(session_version_++)); |
Harald Alvestrand | f01bd6c | 2020-10-23 13:30:46 | [diff] [blame] | 368 | if (sdp_info_->local_description()) { |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 369 | // Include all local ICE candidates in the SessionDescription unless |
| 370 | // the remote peer has requested an ICE restart. |
| 371 | for (const cricket::MediaDescriptionOptions& options : |
| 372 | request.options.media_description_options) { |
| 373 | if (!options.transport_options.ice_restart) { |
Harald Alvestrand | f01bd6c | 2020-10-23 13:30:46 | [diff] [blame] | 374 | CopyCandidatesFromSessionDescription(sdp_info_->local_description(), |
Steve Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 375 | options.mid, answer.get()); |
deadbeef | 0ed85b2 | 2016-02-24 01:24:52 | [diff] [blame] | 376 | } |
| 377 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 378 | } |
Niels Möller | afb246b | 2022-04-20 12:26:50 | [diff] [blame] | 379 | PostCreateSessionDescriptionSucceeded(request.observer.get(), |
| 380 | std::move(answer)); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 381 | } |
| 382 | |
tommi | 0f620f4 | 2015-07-09 10:25:02 | [diff] [blame] | 383 | void WebRtcSessionDescriptionFactory::FailPendingRequests( |
| 384 | const std::string& reason) { |
Tomas Gunnarsson | 20f7456 | 2021-02-04 09:22:50 | [diff] [blame] | 385 | RTC_DCHECK_RUN_ON(signaling_thread_); |
tommi | 0f620f4 | 2015-07-09 10:25:02 | [diff] [blame] | 386 | while (!create_session_description_requests_.empty()) { |
| 387 | const CreateSessionDescriptionRequest& request = |
| 388 | create_session_description_requests_.front(); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 389 | PostCreateSessionDescriptionFailed( |
Niels Möller | afb246b | 2022-04-20 12:26:50 | [diff] [blame] | 390 | request.observer.get(), |
Philipp Hancke | b916a70 | 2023-09-18 11:06:24 | [diff] [blame] | 391 | RTCError(RTCErrorType::INTERNAL_ERROR, |
| 392 | ((request.type == CreateSessionDescriptionRequest::kOffer) |
| 393 | ? "CreateOffer" |
| 394 | : "CreateAnswer") + |
| 395 | reason)); |
tommi | 0f620f4 | 2015-07-09 10:25:02 | [diff] [blame] | 396 | create_session_description_requests_.pop(); |
| 397 | } |
| 398 | } |
| 399 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 400 | void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionFailed( |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 401 | CreateSessionDescriptionObserver* observer, |
Philipp Hancke | b916a70 | 2023-09-18 11:06:24 | [diff] [blame] | 402 | RTCError error) { |
Danil Chapovalov | 97bdfa3 | 2022-08-22 17:45:43 | [diff] [blame] | 403 | Post([observer = |
| 404 | rtc::scoped_refptr<CreateSessionDescriptionObserver>(observer), |
Philipp Hancke | b916a70 | 2023-09-18 11:06:24 | [diff] [blame] | 405 | error]() mutable { observer->OnFailure(error); }); |
| 406 | RTC_LOG(LS_ERROR) << "CreateSessionDescription failed: " << error.message(); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionSucceeded( |
| 410 | CreateSessionDescriptionObserver* observer, |
Steve Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 411 | std::unique_ptr<SessionDescriptionInterface> description) { |
Danil Chapovalov | 97bdfa3 | 2022-08-22 17:45:43 | [diff] [blame] | 412 | Post([observer = |
| 413 | rtc::scoped_refptr<CreateSessionDescriptionObserver>(observer), |
| 414 | description = std::move(description)]() mutable { |
| 415 | observer->OnSuccess(description.release()); |
| 416 | }); |
| 417 | } |
| 418 | |
| 419 | void WebRtcSessionDescriptionFactory::Post( |
| 420 | absl::AnyInvocable<void() &&> callback) { |
| 421 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 422 | callbacks_.push(std::move(callback)); |
| 423 | signaling_thread_->PostTask([weak_ptr = weak_factory_.GetWeakPtr()] { |
| 424 | if (weak_ptr) { |
| 425 | auto& callbacks = weak_ptr->callbacks_; |
| 426 | // Callbacks are pushed from the same thread, thus this task should |
| 427 | // corresond to the first entry in the queue. |
| 428 | RTC_DCHECK(!callbacks.empty()); |
| 429 | std::move(callbacks.front())(); |
| 430 | callbacks.pop(); |
| 431 | } |
| 432 | }); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 433 | } |
| 434 | |
Henrik Boström | d03c23b | 2016-06-01 09:44:18 | [diff] [blame] | 435 | void WebRtcSessionDescriptionFactory::OnCertificateRequestFailed() { |
Tomas Gunnarsson | 20f7456 | 2021-02-04 09:22:50 | [diff] [blame] | 436 | RTC_DCHECK_RUN_ON(signaling_thread_); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 437 | |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 438 | RTC_LOG(LS_ERROR) << "Asynchronous certificate generation request failed."; |
Henrik Boström | 87713d0 | 2015-08-25 07:53:21 | [diff] [blame] | 439 | certificate_request_state_ = CERTIFICATE_FAILED; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 440 | |
tommi | 0f620f4 | 2015-07-09 10:25:02 | [diff] [blame] | 441 | FailPendingRequests(kFailedDueToIdentityFailed); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 442 | } |
| 443 | |
Henrik Boström | d828198 | 2015-08-27 08:12:24 | [diff] [blame] | 444 | void WebRtcSessionDescriptionFactory::SetCertificate( |
Danil Chapovalov | b7da816 | 2022-08-22 14:39:34 | [diff] [blame] | 445 | rtc::scoped_refptr<rtc::RTCCertificate> certificate) { |
henrikg | 91d6ede | 2015-09-17 07:24:34 | [diff] [blame] | 446 | RTC_DCHECK(certificate); |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 447 | RTC_LOG(LS_VERBOSE) << "Setting new certificate."; |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 | [diff] [blame] | 448 | |
Henrik Boström | 87713d0 | 2015-08-25 07:53:21 | [diff] [blame] | 449 | certificate_request_state_ = CERTIFICATE_SUCCEEDED; |
Harald Alvestrand | a094787 | 2020-11-09 14:15:00 | [diff] [blame] | 450 | |
| 451 | on_certificate_ready_(certificate); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 452 | |
Danil Chapovalov | b7da816 | 2022-08-22 14:39:34 | [diff] [blame] | 453 | transport_desc_factory_.set_certificate(std::move(certificate)); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 454 | |
| 455 | while (!create_session_description_requests_.empty()) { |
| 456 | if (create_session_description_requests_.front().type == |
| 457 | CreateSessionDescriptionRequest::kOffer) { |
| 458 | InternalCreateOffer(create_session_description_requests_.front()); |
| 459 | } else { |
| 460 | InternalCreateAnswer(create_session_description_requests_.front()); |
| 461 | } |
| 462 | create_session_description_requests_.pop(); |
| 463 | } |
| 464 | } |
Harald Alvestrand | 974044e | 2024-02-08 13:15:51 | [diff] [blame] | 465 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 466 | } // namespace webrtc |