blob: 7f30b50d97792c5843d97b0c0f6e44a70c7db7dc [file] [log] [blame]
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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:36 +00009 */
10
Steve Anton10542f22019-01-11 09:11:00 -080011#include "api/jsep_session_description.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
kwibergd1fe2812016-04-27 06:47:29 -070013#include <memory>
14
Steve Anton88f2cb92017-12-05 12:47:32 -080015#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080016#include "pc/media_session.h"
17#include "pc/webrtc_sdp.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/arraysize.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020using cricket::SessionDescription;
21
22namespace webrtc {
Steve Anton88f2cb92017-12-05 12:47:32 -080023namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024
zhihuang38989e52017-03-21 11:04:53 -070025// RFC 5245
26// It is RECOMMENDED that default candidates be chosen based on the
27// likelihood of those candidates to work with the peer that is being
28// contacted. It is RECOMMENDED that relayed > reflexive > host.
Steve Anton88f2cb92017-12-05 12:47:32 -080029constexpr int kPreferenceUnknown = 0;
30constexpr int kPreferenceHost = 1;
31constexpr int kPreferenceReflexive = 2;
32constexpr int kPreferenceRelayed = 3;
zhihuang38989e52017-03-21 11:04:53 -070033
Steve Anton88f2cb92017-12-05 12:47:32 -080034constexpr char kDummyAddress[] = "0.0.0.0";
35constexpr int kDummyPort = 9;
zhihuang38989e52017-03-21 11:04:53 -070036
Steve Anton88f2cb92017-12-05 12:47:32 -080037int GetCandidatePreferenceFromType(const std::string& type) {
zhihuang38989e52017-03-21 11:04:53 -070038 int preference = kPreferenceUnknown;
39 if (type == cricket::LOCAL_PORT_TYPE) {
40 preference = kPreferenceHost;
41 } else if (type == cricket::STUN_PORT_TYPE) {
42 preference = kPreferenceReflexive;
43 } else if (type == cricket::RELAY_PORT_TYPE) {
44 preference = kPreferenceRelayed;
45 } else {
zhihuang15238652017-03-23 10:32:12 -070046 preference = kPreferenceUnknown;
zhihuang38989e52017-03-21 11:04:53 -070047 }
48 return preference;
49}
50
51// Update the connection address for the MediaContentDescription based on the
52// candidates.
Steve Anton88f2cb92017-12-05 12:47:32 -080053void UpdateConnectionAddress(
zhihuang38989e52017-03-21 11:04:53 -070054 const JsepCandidateCollection& candidate_collection,
Steve Antonb1c1de12017-12-21 15:14:30 -080055 cricket::MediaContentDescription* media_desc) {
zhihuang38989e52017-03-21 11:04:53 -070056 int port = kDummyPort;
57 std::string ip = kDummyAddress;
Qingsi Wang56991422019-02-08 12:53:06 -080058 std::string hostname;
zhihuang38989e52017-03-21 11:04:53 -070059 int current_preference = kPreferenceUnknown;
60 int current_family = AF_UNSPEC;
61 for (size_t i = 0; i < candidate_collection.count(); ++i) {
62 const IceCandidateInterface* jsep_candidate = candidate_collection.at(i);
63 if (jsep_candidate->candidate().component() !=
64 cricket::ICE_CANDIDATE_COMPONENT_RTP) {
65 continue;
66 }
67 // Default destination should be UDP only.
68 if (jsep_candidate->candidate().protocol() != cricket::UDP_PROTOCOL_NAME) {
69 continue;
70 }
71 const int preference =
72 GetCandidatePreferenceFromType(jsep_candidate->candidate().type());
73 const int family = jsep_candidate->candidate().address().ipaddr().family();
74 // See if this candidate is more preferable then the current one if it's the
75 // same family. Or if the current family is IPv4 already so we could safely
76 // ignore all IPv6 ones. WebRTC bug 4269.
77 // http://code.google.com/p/webrtc/issues/detail?id=4269
78 if ((preference <= current_preference && current_family == family) ||
79 (current_family == AF_INET && family == AF_INET6)) {
80 continue;
81 }
82 current_preference = preference;
83 current_family = family;
Qingsi Wang56991422019-02-08 12:53:06 -080084 const rtc::SocketAddress& candidate_addr =
85 jsep_candidate->candidate().address();
86 port = candidate_addr.port();
87 ip = candidate_addr.ipaddr().ToString();
88 hostname = candidate_addr.hostname();
zhihuang38989e52017-03-21 11:04:53 -070089 }
Qingsi Wang56991422019-02-08 12:53:06 -080090 rtc::SocketAddress connection_addr(ip, port);
91 if (rtc::IPIsUnspec(connection_addr.ipaddr()) && !hostname.empty()) {
Qingsi Wang3ae59d32019-07-11 21:53:45 -070092 // When a hostname candidate becomes the (default) connection address,
93 // we use the dummy address 0.0.0.0 and port 9 in the c= and the m= lines.
94 //
95 // We have observed in deployment that with a FQDN in a c= line, SDP parsing
96 // could fail in other JSEP implementations. We note that the wildcard
97 // addresses (0.0.0.0 or ::) with port 9 are given the exception as the
98 // connection address that will not result in an ICE mismatch
99 // (draft-ietf-mmusic-ice-sip-sdp). Also, 0.0.0.0 or :: can be used as the
100 // connection address in the initial offer or answer with trickle ICE
101 // if the offerer or answerer does not want to include the host IP address
102 // (draft-ietf-mmusic-trickle-ice-sip), and in particular 0.0.0.0 has been
103 // widely deployed for this use without outstanding compatibility issues.
104 // Combining the above considerations, we use 0.0.0.0 with port 9 to
105 // populate the c= and the m= lines. See |BuildMediaDescription| in
106 // webrtc_sdp.cc for the SDP generation with
107 // |media_desc->connection_address()|.
108 connection_addr = rtc::SocketAddress(kDummyAddress, kDummyPort);
Qingsi Wang56991422019-02-08 12:53:06 -0800109 }
Steve Antonb1c1de12017-12-21 15:14:30 -0800110 media_desc->set_connection_address(connection_addr);
zhihuang38989e52017-03-21 11:04:53 -0700111}
112
Steve Anton88f2cb92017-12-05 12:47:32 -0800113} // namespace
114
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115const int JsepSessionDescription::kDefaultVideoCodecId = 100;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116const char JsepSessionDescription::kDefaultVideoCodecName[] = "VP8";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117
Steve Anton88f2cb92017-12-05 12:47:32 -0800118// TODO(steveanton): Remove this default implementation once Chromium has been
119// updated.
120SdpType SessionDescriptionInterface::GetType() const {
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200121 absl::optional<SdpType> maybe_type = SdpTypeFromString(type());
Steve Anton88f2cb92017-12-05 12:47:32 -0800122 if (maybe_type) {
123 return *maybe_type;
124 } else {
125 RTC_LOG(LS_WARNING) << "Default implementation of "
126 "SessionDescriptionInterface::GetType does not "
127 "recognize the result from type(), returning "
128 "kOffer.";
129 return SdpType::kOffer;
130 }
131}
132
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133SessionDescriptionInterface* CreateSessionDescription(const std::string& type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 const std::string& sdp,
135 SdpParseError* error) {
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200136 absl::optional<SdpType> maybe_type = SdpTypeFromString(type);
Steve Anton88f2cb92017-12-05 12:47:32 -0800137 if (!maybe_type) {
138 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 }
140
Steve Anton88f2cb92017-12-05 12:47:32 -0800141 return CreateSessionDescription(*maybe_type, sdp, error).release();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142}
143
Steve Anton88f2cb92017-12-05 12:47:32 -0800144std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
145 SdpType type,
146 const std::string& sdp) {
147 return CreateSessionDescription(type, sdp, nullptr);
148}
149
150std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
151 SdpType type,
152 const std::string& sdp,
153 SdpParseError* error_out) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200154 auto jsep_desc = std::make_unique<JsepSessionDescription>(type);
Eldar Rello5ab79e62019-10-09 18:29:44 +0300155 if (type != SdpType::kRollback) {
156 if (!SdpDeserialize(sdp, jsep_desc.get(), error_out)) {
157 return nullptr;
158 }
Steve Anton88f2cb92017-12-05 12:47:32 -0800159 }
160 return std::move(jsep_desc);
161}
162
Steve Antond9e4a062018-07-24 18:23:33 -0700163std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
164 SdpType type,
165 const std::string& session_id,
166 const std::string& session_version,
167 std::unique_ptr<cricket::SessionDescription> description) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200168 auto jsep_description = std::make_unique<JsepSessionDescription>(type);
Steve Antond9e4a062018-07-24 18:23:33 -0700169 bool initialize_success = jsep_description->Initialize(
Harald Alvestrand4d7160e2019-04-12 07:01:29 +0200170 std::move(description), session_id, session_version);
Steve Antond9e4a062018-07-24 18:23:33 -0700171 RTC_DCHECK(initialize_success);
172 return std::move(jsep_description);
173}
174
Steve Anton88f2cb92017-12-05 12:47:32 -0800175JsepSessionDescription::JsepSessionDescription(SdpType type) : type_(type) {}
176
177JsepSessionDescription::JsepSessionDescription(const std::string& type) {
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200178 absl::optional<SdpType> maybe_type = SdpTypeFromString(type);
Steve Anton88f2cb92017-12-05 12:47:32 -0800179 if (maybe_type) {
180 type_ = *maybe_type;
181 } else {
182 RTC_LOG(LS_WARNING)
183 << "JsepSessionDescription constructed with invalid type string: "
184 << type << ". Assuming it is an offer.";
185 type_ = SdpType::kOffer;
186 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187}
188
Steve Anton6fe1fba2018-12-11 10:15:23 -0800189JsepSessionDescription::JsepSessionDescription(
190 SdpType type,
191 std::unique_ptr<cricket::SessionDescription> description,
192 absl::string_view session_id,
193 absl::string_view session_version)
194 : description_(std::move(description)),
195 session_id_(session_id),
196 session_version_(session_version),
197 type_(type) {
198 RTC_DCHECK(description_);
199 candidate_collection_.resize(number_of_mediasections());
200}
201
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202JsepSessionDescription::~JsepSessionDescription() {}
203
204bool JsepSessionDescription::Initialize(
Harald Alvestrand4d7160e2019-04-12 07:01:29 +0200205 std::unique_ptr<cricket::SessionDescription> description,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 const std::string& session_id,
207 const std::string& session_version) {
208 if (!description)
209 return false;
210
211 session_id_ = session_id;
212 session_version_ = session_version;
Harald Alvestrand4d7160e2019-04-12 07:01:29 +0200213 description_ = std::move(description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 candidate_collection_.resize(number_of_mediasections());
215 return true;
216}
217
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218bool JsepSessionDescription::AddCandidate(
219 const IceCandidateInterface* candidate) {
Guido Urdaneta41633172019-05-23 20:12:29 +0200220 if (!candidate)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221 return false;
222 size_t mediasection_index = 0;
223 if (!GetMediasectionIndex(candidate, &mediasection_index)) {
224 return false;
225 }
226 if (mediasection_index >= number_of_mediasections())
227 return false;
jbauch083b73f2015-07-16 02:46:32 -0700228 const std::string& content_name =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229 description_->contents()[mediasection_index].name;
230 const cricket::TransportInfo* transport_info =
231 description_->GetTransportInfoByName(content_name);
232 if (!transport_info) {
233 return false;
234 }
235
236 cricket::Candidate updated_candidate = candidate->candidate();
237 if (updated_candidate.username().empty()) {
238 updated_candidate.set_username(transport_info->description.ice_ufrag);
239 }
240 if (updated_candidate.password().empty()) {
241 updated_candidate.set_password(transport_info->description.ice_pwd);
242 }
243
kwibergd1fe2812016-04-27 06:47:29 -0700244 std::unique_ptr<JsepIceCandidate> updated_candidate_wrapper(
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000245 new JsepIceCandidate(candidate->sdp_mid(),
246 static_cast<int>(mediasection_index),
247 updated_candidate));
248 if (!candidate_collection_[mediasection_index].HasCandidate(
zhihuang38989e52017-03-21 11:04:53 -0700249 updated_candidate_wrapper.get())) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000250 candidate_collection_[mediasection_index].add(
251 updated_candidate_wrapper.release());
zhihuang38989e52017-03-21 11:04:53 -0700252 UpdateConnectionAddress(
253 candidate_collection_[mediasection_index],
Steve Antonb1c1de12017-12-21 15:14:30 -0800254 description_->contents()[mediasection_index].media_description());
zhihuang38989e52017-03-21 11:04:53 -0700255 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000256
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257 return true;
258}
259
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700260size_t JsepSessionDescription::RemoveCandidates(
261 const std::vector<cricket::Candidate>& candidates) {
262 size_t num_removed = 0;
263 for (auto& candidate : candidates) {
264 int mediasection_index = GetMediasectionIndex(candidate);
265 if (mediasection_index < 0) {
266 // Not found.
267 continue;
268 }
269 num_removed += candidate_collection_[mediasection_index].remove(candidate);
zhihuang38989e52017-03-21 11:04:53 -0700270 UpdateConnectionAddress(
271 candidate_collection_[mediasection_index],
Steve Antonb1c1de12017-12-21 15:14:30 -0800272 description_->contents()[mediasection_index].media_description());
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700273 }
274 return num_removed;
275}
276
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277size_t JsepSessionDescription::number_of_mediasections() const {
278 if (!description_)
279 return 0;
280 return description_->contents().size();
281}
282
283const IceCandidateCollection* JsepSessionDescription::candidates(
284 size_t mediasection_index) const {
285 if (mediasection_index >= candidate_collection_.size())
286 return NULL;
287 return &candidate_collection_[mediasection_index];
288}
289
290bool JsepSessionDescription::ToString(std::string* out) const {
deadbeef9d3584c2016-02-16 17:54:10 -0800291 if (!description_ || !out) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000292 return false;
deadbeef9d3584c2016-02-16 17:54:10 -0800293 }
Steve Antone831b8c2018-02-01 12:22:16 -0800294 *out = SdpSerialize(*this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295 return !out->empty();
296}
297
298bool JsepSessionDescription::GetMediasectionIndex(
299 const IceCandidateInterface* candidate,
300 size_t* index) {
301 if (!candidate || !index) {
302 return false;
303 }
Guido Urdaneta41633172019-05-23 20:12:29 +0200304
305 // If the candidate has no valid mline index or sdp_mid, it is impossible
306 // to find a match.
307 if (candidate->sdp_mid().empty() &&
308 (candidate->sdp_mline_index() < 0 ||
309 static_cast<size_t>(candidate->sdp_mline_index()) >=
310 description_->contents().size())) {
311 return false;
312 }
313
314 if (candidate->sdp_mline_index() >= 0)
315 *index = static_cast<size_t>(candidate->sdp_mline_index());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316 if (description_ && !candidate->sdp_mid().empty()) {
317 bool found = false;
318 // Try to match the sdp_mid with content name.
319 for (size_t i = 0; i < description_->contents().size(); ++i) {
320 if (candidate->sdp_mid() == description_->contents().at(i).name) {
321 *index = i;
322 found = true;
323 break;
324 }
325 }
326 if (!found) {
327 // If the sdp_mid is presented but we can't find a match, we consider
328 // this as an error.
329 return false;
330 }
331 }
332 return true;
333}
334
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700335int JsepSessionDescription::GetMediasectionIndex(
336 const cricket::Candidate& candidate) {
337 // Find the description with a matching transport name of the candidate.
338 const std::string& transport_name = candidate.transport_name();
339 for (size_t i = 0; i < description_->contents().size(); ++i) {
340 if (transport_name == description_->contents().at(i).name) {
341 return static_cast<int>(i);
342 }
343 }
344 return -1;
345}
346
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347} // namespace webrtc