blob: db54dbccdab93cab7d3895cfa42ff5c3b7c1909e [file] [log] [blame]
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:131/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:363 *
kjellanderb24317b2016-02-10 15:54:434 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:369 */
10
Steve Anton10542f22019-01-11 17:11:0011#include "api/jsep_session_description.h"
henrike@webrtc.org28e20752013-07-10 00:45:3612
kwibergd1fe2812016-04-27 13:47:2913#include <memory>
Harald Alvestrandc24a2182022-02-23 13:44:5914#include <utility>
kwibergd1fe2812016-04-27 13:47:2915
Harald Alvestrandc24a2182022-02-23 13:44:5916#include "absl/types/optional.h"
17#include "p2p/base/p2p_constants.h"
Steve Anton88f2cb92017-12-05 20:47:3218#include "p2p/base/port.h"
Harald Alvestrandc24a2182022-02-23 13:44:5919#include "p2p/base/transport_description.h"
20#include "p2p/base/transport_info.h"
21#include "pc/media_session.h" // IWYU pragma: keep
Steve Anton10542f22019-01-11 17:11:0022#include "pc/webrtc_sdp.h"
Harald Alvestrandc24a2182022-02-23 13:44:5923#include "rtc_base/checks.h"
24#include "rtc_base/ip_address.h"
25#include "rtc_base/logging.h"
26#include "rtc_base/net_helper.h"
27#include "rtc_base/socket_address.h"
henrike@webrtc.org28e20752013-07-10 00:45:3628
Tommi0a7fc842024-01-19 12:11:3729using cricket::Candidate;
henrike@webrtc.org28e20752013-07-10 00:45:3630using cricket::SessionDescription;
31
32namespace webrtc {
Steve Anton88f2cb92017-12-05 20:47:3233namespace {
henrike@webrtc.org28e20752013-07-10 00:45:3634
zhihuang38989e52017-03-21 18:04:5335// RFC 5245
36// It is RECOMMENDED that default candidates be chosen based on the
37// likelihood of those candidates to work with the peer that is being
38// contacted. It is RECOMMENDED that relayed > reflexive > host.
Steve Anton88f2cb92017-12-05 20:47:3239constexpr int kPreferenceUnknown = 0;
40constexpr int kPreferenceHost = 1;
41constexpr int kPreferenceReflexive = 2;
42constexpr int kPreferenceRelayed = 3;
zhihuang38989e52017-03-21 18:04:5343
Steve Anton88f2cb92017-12-05 20:47:3244constexpr char kDummyAddress[] = "0.0.0.0";
45constexpr int kDummyPort = 9;
zhihuang38989e52017-03-21 18:04:5346
Tommi0a7fc842024-01-19 12:11:3747int GetCandidatePreferenceFromType(const Candidate& c) {
zhihuang38989e52017-03-21 18:04:5348 int preference = kPreferenceUnknown;
Tommi0a7fc842024-01-19 12:11:3749 if (c.is_local()) {
zhihuang38989e52017-03-21 18:04:5350 preference = kPreferenceHost;
Tommi0a7fc842024-01-19 12:11:3751 } else if (c.is_stun()) {
zhihuang38989e52017-03-21 18:04:5352 preference = kPreferenceReflexive;
Tommi0a7fc842024-01-19 12:11:3753 } else if (c.is_relay()) {
zhihuang38989e52017-03-21 18:04:5354 preference = kPreferenceRelayed;
55 } else {
zhihuang15238652017-03-23 17:32:1256 preference = kPreferenceUnknown;
zhihuang38989e52017-03-21 18:04:5357 }
58 return preference;
59}
60
61// Update the connection address for the MediaContentDescription based on the
62// candidates.
Steve Anton88f2cb92017-12-05 20:47:3263void UpdateConnectionAddress(
zhihuang38989e52017-03-21 18:04:5364 const JsepCandidateCollection& candidate_collection,
Steve Antonb1c1de12017-12-21 23:14:3065 cricket::MediaContentDescription* media_desc) {
zhihuang38989e52017-03-21 18:04:5366 int port = kDummyPort;
67 std::string ip = kDummyAddress;
Qingsi Wang56991422019-02-08 20:53:0668 std::string hostname;
zhihuang38989e52017-03-21 18:04:5369 int current_preference = kPreferenceUnknown;
70 int current_family = AF_UNSPEC;
71 for (size_t i = 0; i < candidate_collection.count(); ++i) {
72 const IceCandidateInterface* jsep_candidate = candidate_collection.at(i);
73 if (jsep_candidate->candidate().component() !=
74 cricket::ICE_CANDIDATE_COMPONENT_RTP) {
75 continue;
76 }
77 // Default destination should be UDP only.
78 if (jsep_candidate->candidate().protocol() != cricket::UDP_PROTOCOL_NAME) {
79 continue;
80 }
81 const int preference =
Tommi0a7fc842024-01-19 12:11:3782 GetCandidatePreferenceFromType(jsep_candidate->candidate());
zhihuang38989e52017-03-21 18:04:5383 const int family = jsep_candidate->candidate().address().ipaddr().family();
84 // See if this candidate is more preferable then the current one if it's the
85 // same family. Or if the current family is IPv4 already so we could safely
86 // ignore all IPv6 ones. WebRTC bug 4269.
87 // http://code.google.com/p/webrtc/issues/detail?id=4269
88 if ((preference <= current_preference && current_family == family) ||
89 (current_family == AF_INET && family == AF_INET6)) {
90 continue;
91 }
92 current_preference = preference;
93 current_family = family;
Qingsi Wang56991422019-02-08 20:53:0694 const rtc::SocketAddress& candidate_addr =
95 jsep_candidate->candidate().address();
96 port = candidate_addr.port();
97 ip = candidate_addr.ipaddr().ToString();
98 hostname = candidate_addr.hostname();
zhihuang38989e52017-03-21 18:04:5399 }
Qingsi Wang56991422019-02-08 20:53:06100 rtc::SocketAddress connection_addr(ip, port);
101 if (rtc::IPIsUnspec(connection_addr.ipaddr()) && !hostname.empty()) {
Qingsi Wang3ae59d32019-07-12 04:53:45102 // When a hostname candidate becomes the (default) connection address,
103 // we use the dummy address 0.0.0.0 and port 9 in the c= and the m= lines.
104 //
105 // We have observed in deployment that with a FQDN in a c= line, SDP parsing
106 // could fail in other JSEP implementations. We note that the wildcard
107 // addresses (0.0.0.0 or ::) with port 9 are given the exception as the
108 // connection address that will not result in an ICE mismatch
109 // (draft-ietf-mmusic-ice-sip-sdp). Also, 0.0.0.0 or :: can be used as the
110 // connection address in the initial offer or answer with trickle ICE
111 // if the offerer or answerer does not want to include the host IP address
112 // (draft-ietf-mmusic-trickle-ice-sip), and in particular 0.0.0.0 has been
113 // widely deployed for this use without outstanding compatibility issues.
114 // Combining the above considerations, we use 0.0.0.0 with port 9 to
Artem Titov880fa812021-07-30 20:30:23115 // populate the c= and the m= lines. See `BuildMediaDescription` in
Qingsi Wang3ae59d32019-07-12 04:53:45116 // webrtc_sdp.cc for the SDP generation with
Artem Titovcfea2182021-08-09 23:22:31117 // `media_desc->connection_address()`.
Qingsi Wang3ae59d32019-07-12 04:53:45118 connection_addr = rtc::SocketAddress(kDummyAddress, kDummyPort);
Qingsi Wang56991422019-02-08 20:53:06119 }
Steve Antonb1c1de12017-12-21 23:14:30120 media_desc->set_connection_address(connection_addr);
zhihuang38989e52017-03-21 18:04:53121}
122
Steve Anton88f2cb92017-12-05 20:47:32123} // namespace
124
Steve Anton88f2cb92017-12-05 20:47:32125// TODO(steveanton): Remove this default implementation once Chromium has been
126// updated.
127SdpType SessionDescriptionInterface::GetType() const {
Danil Chapovalov66cadcc2018-06-19 14:47:43128 absl::optional<SdpType> maybe_type = SdpTypeFromString(type());
Steve Anton88f2cb92017-12-05 20:47:32129 if (maybe_type) {
130 return *maybe_type;
131 } else {
132 RTC_LOG(LS_WARNING) << "Default implementation of "
133 "SessionDescriptionInterface::GetType does not "
134 "recognize the result from type(), returning "
135 "kOffer.";
136 return SdpType::kOffer;
137 }
138}
139
henrike@webrtc.org28e20752013-07-10 00:45:36140SessionDescriptionInterface* CreateSessionDescription(const std::string& type,
henrike@webrtc.org28e20752013-07-10 00:45:36141 const std::string& sdp,
142 SdpParseError* error) {
Danil Chapovalov66cadcc2018-06-19 14:47:43143 absl::optional<SdpType> maybe_type = SdpTypeFromString(type);
Steve Anton88f2cb92017-12-05 20:47:32144 if (!maybe_type) {
145 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36146 }
147
Steve Anton88f2cb92017-12-05 20:47:32148 return CreateSessionDescription(*maybe_type, sdp, error).release();
henrike@webrtc.org28e20752013-07-10 00:45:36149}
150
Steve Anton88f2cb92017-12-05 20:47:32151std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
152 SdpType type,
153 const std::string& sdp) {
154 return CreateSessionDescription(type, sdp, nullptr);
155}
156
157std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
158 SdpType type,
159 const std::string& sdp,
160 SdpParseError* error_out) {
Mirko Bonadei317a1f02019-09-17 15:06:18161 auto jsep_desc = std::make_unique<JsepSessionDescription>(type);
Eldar Rello5ab79e62019-10-09 15:29:44162 if (type != SdpType::kRollback) {
163 if (!SdpDeserialize(sdp, jsep_desc.get(), error_out)) {
164 return nullptr;
165 }
Steve Anton88f2cb92017-12-05 20:47:32166 }
167 return std::move(jsep_desc);
168}
169
Steve Antond9e4a062018-07-25 01:23:33170std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
171 SdpType type,
172 const std::string& session_id,
173 const std::string& session_version,
174 std::unique_ptr<cricket::SessionDescription> description) {
Mirko Bonadei317a1f02019-09-17 15:06:18175 auto jsep_description = std::make_unique<JsepSessionDescription>(type);
Steve Antond9e4a062018-07-25 01:23:33176 bool initialize_success = jsep_description->Initialize(
Harald Alvestrand4d7160e2019-04-12 05:01:29177 std::move(description), session_id, session_version);
Steve Antond9e4a062018-07-25 01:23:33178 RTC_DCHECK(initialize_success);
179 return std::move(jsep_description);
180}
181
Steve Anton88f2cb92017-12-05 20:47:32182JsepSessionDescription::JsepSessionDescription(SdpType type) : type_(type) {}
183
184JsepSessionDescription::JsepSessionDescription(const std::string& type) {
Danil Chapovalov66cadcc2018-06-19 14:47:43185 absl::optional<SdpType> maybe_type = SdpTypeFromString(type);
Steve Anton88f2cb92017-12-05 20:47:32186 if (maybe_type) {
187 type_ = *maybe_type;
188 } else {
189 RTC_LOG(LS_WARNING)
190 << "JsepSessionDescription constructed with invalid type string: "
191 << type << ". Assuming it is an offer.";
192 type_ = SdpType::kOffer;
193 }
henrike@webrtc.org28e20752013-07-10 00:45:36194}
195
Steve Anton6fe1fba2018-12-11 18:15:23196JsepSessionDescription::JsepSessionDescription(
197 SdpType type,
198 std::unique_ptr<cricket::SessionDescription> description,
199 absl::string_view session_id,
200 absl::string_view session_version)
201 : description_(std::move(description)),
202 session_id_(session_id),
203 session_version_(session_version),
204 type_(type) {
205 RTC_DCHECK(description_);
206 candidate_collection_.resize(number_of_mediasections());
207}
208
henrike@webrtc.org28e20752013-07-10 00:45:36209JsepSessionDescription::~JsepSessionDescription() {}
210
211bool JsepSessionDescription::Initialize(
Harald Alvestrand4d7160e2019-04-12 05:01:29212 std::unique_ptr<cricket::SessionDescription> description,
henrike@webrtc.org28e20752013-07-10 00:45:36213 const std::string& session_id,
214 const std::string& session_version) {
215 if (!description)
216 return false;
217
218 session_id_ = session_id;
219 session_version_ = session_version;
Harald Alvestrand4d7160e2019-04-12 05:01:29220 description_ = std::move(description);
henrike@webrtc.org28e20752013-07-10 00:45:36221 candidate_collection_.resize(number_of_mediasections());
222 return true;
223}
224
Harald Alvestrandc908f1c2020-12-17 19:28:29225std::unique_ptr<SessionDescriptionInterface> JsepSessionDescription::Clone()
226 const {
Harald Alvestrand0e7b3a92020-12-15 15:18:28227 auto new_description = std::make_unique<JsepSessionDescription>(type_);
Harald Alvestrandfc6b8712021-01-05 10:51:08228 new_description->session_id_ = session_id_;
229 new_description->session_version_ = session_version_;
Byoungchan Leecb42e102021-07-15 05:09:40230 if (description_) {
231 new_description->description_ = description_->Clone();
232 }
Harald Alvestrandfc6b8712021-01-05 10:51:08233 for (const auto& collection : candidate_collection_) {
234 new_description->candidate_collection_.push_back(collection.Clone());
235 }
Harald Alvestrand0e7b3a92020-12-15 15:18:28236 return new_description;
237}
238
henrike@webrtc.org28e20752013-07-10 00:45:36239bool JsepSessionDescription::AddCandidate(
240 const IceCandidateInterface* candidate) {
Guido Urdaneta41633172019-05-23 18:12:29241 if (!candidate)
henrike@webrtc.org28e20752013-07-10 00:45:36242 return false;
243 size_t mediasection_index = 0;
244 if (!GetMediasectionIndex(candidate, &mediasection_index)) {
245 return false;
246 }
247 if (mediasection_index >= number_of_mediasections())
248 return false;
jbauch083b73f2015-07-16 09:46:32249 const std::string& content_name =
henrike@webrtc.org28e20752013-07-10 00:45:36250 description_->contents()[mediasection_index].name;
251 const cricket::TransportInfo* transport_info =
252 description_->GetTransportInfoByName(content_name);
253 if (!transport_info) {
254 return false;
255 }
256
Tommi0a7fc842024-01-19 12:11:37257 Candidate updated_candidate = candidate->candidate();
henrike@webrtc.org28e20752013-07-10 00:45:36258 if (updated_candidate.username().empty()) {
259 updated_candidate.set_username(transport_info->description.ice_ufrag);
260 }
261 if (updated_candidate.password().empty()) {
262 updated_candidate.set_password(transport_info->description.ice_pwd);
263 }
264
kwibergd1fe2812016-04-27 13:47:29265 std::unique_ptr<JsepIceCandidate> updated_candidate_wrapper(
mallinath@webrtc.org67ee6b92014-02-03 16:57:16266 new JsepIceCandidate(candidate->sdp_mid(),
267 static_cast<int>(mediasection_index),
268 updated_candidate));
269 if (!candidate_collection_[mediasection_index].HasCandidate(
zhihuang38989e52017-03-21 18:04:53270 updated_candidate_wrapper.get())) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16271 candidate_collection_[mediasection_index].add(
272 updated_candidate_wrapper.release());
zhihuang38989e52017-03-21 18:04:53273 UpdateConnectionAddress(
274 candidate_collection_[mediasection_index],
Steve Antonb1c1de12017-12-21 23:14:30275 description_->contents()[mediasection_index].media_description());
zhihuang38989e52017-03-21 18:04:53276 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16277
henrike@webrtc.org28e20752013-07-10 00:45:36278 return true;
279}
280
Honghai Zhang7fb69db2016-03-14 18:59:18281size_t JsepSessionDescription::RemoveCandidates(
Tommi0a7fc842024-01-19 12:11:37282 const std::vector<Candidate>& candidates) {
Honghai Zhang7fb69db2016-03-14 18:59:18283 size_t num_removed = 0;
284 for (auto& candidate : candidates) {
285 int mediasection_index = GetMediasectionIndex(candidate);
286 if (mediasection_index < 0) {
287 // Not found.
288 continue;
289 }
290 num_removed += candidate_collection_[mediasection_index].remove(candidate);
zhihuang38989e52017-03-21 18:04:53291 UpdateConnectionAddress(
292 candidate_collection_[mediasection_index],
Steve Antonb1c1de12017-12-21 23:14:30293 description_->contents()[mediasection_index].media_description());
Honghai Zhang7fb69db2016-03-14 18:59:18294 }
295 return num_removed;
296}
297
henrike@webrtc.org28e20752013-07-10 00:45:36298size_t JsepSessionDescription::number_of_mediasections() const {
299 if (!description_)
300 return 0;
301 return description_->contents().size();
302}
303
304const IceCandidateCollection* JsepSessionDescription::candidates(
305 size_t mediasection_index) const {
306 if (mediasection_index >= candidate_collection_.size())
307 return NULL;
308 return &candidate_collection_[mediasection_index];
309}
310
311bool JsepSessionDescription::ToString(std::string* out) const {
deadbeef9d3584c2016-02-17 01:54:10312 if (!description_ || !out) {
henrike@webrtc.org28e20752013-07-10 00:45:36313 return false;
deadbeef9d3584c2016-02-17 01:54:10314 }
Steve Antone831b8c2018-02-01 20:22:16315 *out = SdpSerialize(*this);
henrike@webrtc.org28e20752013-07-10 00:45:36316 return !out->empty();
317}
318
319bool JsepSessionDescription::GetMediasectionIndex(
320 const IceCandidateInterface* candidate,
321 size_t* index) {
322 if (!candidate || !index) {
323 return false;
324 }
Guido Urdaneta41633172019-05-23 18:12:29325
326 // If the candidate has no valid mline index or sdp_mid, it is impossible
327 // to find a match.
328 if (candidate->sdp_mid().empty() &&
329 (candidate->sdp_mline_index() < 0 ||
330 static_cast<size_t>(candidate->sdp_mline_index()) >=
331 description_->contents().size())) {
332 return false;
333 }
334
335 if (candidate->sdp_mline_index() >= 0)
336 *index = static_cast<size_t>(candidate->sdp_mline_index());
henrike@webrtc.org28e20752013-07-10 00:45:36337 if (description_ && !candidate->sdp_mid().empty()) {
338 bool found = false;
339 // Try to match the sdp_mid with content name.
340 for (size_t i = 0; i < description_->contents().size(); ++i) {
341 if (candidate->sdp_mid() == description_->contents().at(i).name) {
342 *index = i;
343 found = true;
344 break;
345 }
346 }
347 if (!found) {
348 // If the sdp_mid is presented but we can't find a match, we consider
349 // this as an error.
350 return false;
351 }
352 }
353 return true;
354}
355
Tommi0a7fc842024-01-19 12:11:37356int JsepSessionDescription::GetMediasectionIndex(const Candidate& candidate) {
Honghai Zhang7fb69db2016-03-14 18:59:18357 // Find the description with a matching transport name of the candidate.
358 const std::string& transport_name = candidate.transport_name();
359 for (size_t i = 0; i < description_->contents().size(); ++i) {
360 if (transport_name == description_->contents().at(i).name) {
361 return static_cast<int>(i);
362 }
363 }
364 return -1;
365}
366
henrike@webrtc.org28e20752013-07-10 00:45:36367} // namespace webrtc